Skip to content

Commit 5d26daf

Browse files
committed
Fix #1523
1 parent 96cd153 commit 5d26daf

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Improve reporter deprecation messages (#1493, @dakvid).
77
* Clarify scope of `tar_renv()` (#1506, @valentingar).
88
* Handle errors in `rstudioapi::isAvailable()` (#1519, @dipterix).
9+
* Improve error message when metadata file is corrupted (#1523, @dakvid).
910

1011
# targets 1.11.3
1112

R/class_database.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,23 +528,28 @@ database_class <- R6::R6Class(
528528
return()
529529
}
530530
line <- readLines(self$path, n = 1L)
531-
header <- strsplit(line, split = database_sep_outer, fixed = TRUE)[[1]]
531+
header <- if_any(
532+
length(line) > 0L,
533+
strsplit(line, split = database_sep_outer, fixed = TRUE)[[1]],
534+
""
535+
)
532536
if (identical(header, self$header)) {
533537
return()
534538
}
535539
tar_throw_file(
536-
"invalid header in ",
540+
"invalid header in the {targets} database file ",
537541
self$path,
538542
"\n",
539543
" found: ",
540544
paste(header, collapse = database_sep_outer),
541545
"\n",
542546
" expected: ",
543547
paste(self$header, collapse = database_sep_outer),
544-
"\nProbably because of a breaking change in the targets package. ",
545-
"Before running tar_make() again, ",
546-
"either delete the data store with tar_destroy() ",
547-
"or downgrade the targets package to an earlier version."
548+
"\nEither there was a breaking change in the {targets} package ",
549+
"or the metadata file was corrupted somehow. ",
550+
"Before running tar_make() again, either delete the aforementioned ",
551+
"database file, or if applicable, ",
552+
"downgrade the {targets} package to an earlier version."
548553
)
549554
},
550555
validate = function() {

R/class_meta.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ meta_class <- R6::R6Class(
182182
return()
183183
}
184184
line <- readLines(self$database$path, n = 1)
185+
if (!length(line)) {
186+
return()
187+
}
185188
line <- strsplit(line, split = database_sep_outer, fixed = TRUE)[[1]]
186189
if ("repository" %in% line) {
187190
return()

tests/testthat/test-class_meta.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,18 @@ tar_test("migrate meta database", {
351351
expect_equal(tar_read(x), "value")
352352
})
353353

354+
tar_test("migrate meta database", {
355+
temp <- tempfile()
356+
meta <- meta_init(path_store = temp)
357+
on.exit({
358+
meta$database$close()
359+
unlink(temp, recursive = TRUE)
360+
})
361+
dir.create(dirname(meta$database$path), recursive = TRUE)
362+
file.create(meta$database$path)
363+
expect_no_error(meta$migrate_database())
364+
})
365+
354366
tar_test("meta$preprocess() on empty data", {
355367
for (write in c(TRUE, FALSE)) {
356368
meta <- meta_init(path_store = tempfile())

0 commit comments

Comments
 (0)