google / google/copybara

java.nio.file.InvalidPathException when copying a repo that had a file with an invalid character in its name in Windows

Open
#234 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
3.8k
Forks
345
PR merge metrics
No merged PRs in 30d

Description

If a file had an invalid character in its name at some point, then copybara will fail to copy the repo if the workflow specifies `origin_files`.

I made a [repo](https://github.com/g-ortuno/copybara-bug) to reproduce the issue; it also has a copy.bara.sky file that repros the issue. There's a file there, `file with less-than and greater than.txt`, that used to be called `file with <>.txt`. The file has since been renamed but trying to copybara the repo still fails.

Stacktrace:
```
java.nio.file.InvalidPathException: Illegal char <<> at index 11: /file with <>.txt
at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232)
at java.base/java.nio.file.Path.of(Path.java:148)
at java.base/java.nio.file.Paths.get(Paths.java:69)
at com.google.copybara.WorkflowRunHelper$ChangeMigrator.shouldSkipChange(WorkflowRunHelper.java:339)
at com.google.copybara.WorkflowRunHelper$ChangeMigrator.skipChange(WorkflowRunHelper.java:317)
at com.google.copybara.WorkflowMode.lambda$filterChanges$1(WorkflowMode.java:502)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:1006)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
at com.google.copybara.WorkflowMode.filterChanges(WorkflowMode.java:503)
at com.google.copybara.WorkflowMode$1.run(WorkflowMode.java:110)
at com.google.copybara.Workflow.run(Workflow.java:288)
at com.google.copybara.MigrateCmd.run(MigrateCmd.java:92)
at com.google.copybara.MigrateCmd.run(MigrateCmd.java:69)
at com.google.copybara.Main.runInternal(Main.java:242)
at com.google.copybara.Main.run(Main.java:124)
at com.google.copybara.Main.main(Main.java:103)
ERROR: Unexpected error (please file a bug against copybara): Illegal char <<> at index 11: /file with <>.txt (java.nio.file.InvalidPathException: Illegal char <<> at index 11: /file with <>.txt)
```

I found this while trying to copybara the TypeScript repo. I found the issue on Windows, not sure if it repros on other platforms. Probably not since they probably don't use `WindowsPathParser`.

I worked around locally by catching the error in WorkflowRunHelper.java:317:
```
// From:
for (String changedFile : currentChange.getChangeFiles()) {
if (pathMatcher.matches(Paths.get("/" + changedFile))) {
return false;
}
}
// To:
for (String changedFile : currentChange.getChangeFiles()) {
// `changedFile` might not be a valid path on some platforms.
try {
if (pathMatcher.matches(Paths.get("/" + changedFile))) {
return false;
}
} catch (InvalidPathException e) {
workflow.getConsole().warn("Couldn't parse path for file: " + changedFile);
}
}
```
Worked in my case, but probably not a good fix.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.