Skip to content

Commit 65a3f65

Browse files
authored
Merge pull request #7 from stackb/fix/ensure-dirs
fix: always create the workspace runfiles directory in the fallback tar
2 parents 3acbce1 + b3f69cc commit 65a3f65

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

cmd/jar_layerer/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func main() {
3434
flag.Var(&artifactGroupLayers, "artifact_group_layer", "ID1,ID2,...=path.tar (repeatable)")
3535
var padLayers repeatedFlag
3636
flag.Var(&padLayers, "pad_layer", "path to write an empty layer tar (repeatable)")
37+
var ensureDirs repeatedFlag
38+
flag.Var(&ensureDirs, "ensure_dir", "directory entry to always create in the fallback tar (repeatable)")
3739

3840
flag.Parse()
3941

@@ -48,6 +50,7 @@ func main() {
4850
ClasspathPath: *classpath,
4951
AppPrefix: *appPrefix,
5052
PathPrefix: *pathPrefix,
53+
EnsureDirs: ensureDirs,
5154
}
5255

5356
// Read JAR list from file.

jvm_jar_layers.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ def _jvm_jar_layers_impl(ctx):
277277
args.add("--fallback", fallback)
278278
tar_outputs.append(fallback)
279279

280+
# Always materialize the workspace runfiles directory: consumers commonly
281+
# set the image WorkingDir to /app/<workspace> for runfiles-relative
282+
# flags, and OCI runtimes fail chdir when the directory is absent (it is
283+
# otherwise only created when data runfiles exist).
284+
args.add("--ensure_dir", "app/" + ctx.workspace_name)
285+
280286
# Maven artifact layers via aspect.
281287
if ctx.file.maven_lock_file:
282288
lock_file = ctx.file.maven_lock_file

pkg/jarlayer/jarlayer.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type LayerOptions struct {
3232
AppPrefix string
3333
// PathPrefix is prepended to tar entry paths (e.g., "app/lib/").
3434
PathPrefix string
35+
// EnsureDirs lists directories always created in the fallback tar (e.g.
36+
// "app/_main"), so that an image WorkingDir pointing at them exists even
37+
// when no other entry would create them. OCI runtimes fail chdir on a
38+
// missing working directory.
39+
EnsureDirs []string
3540
}
3641

3742
// ArtifactLayer maps one or more artifact IDs to a single output tar.
@@ -108,6 +113,12 @@ func LayerJars(opts LayerOptions) (retErr error) {
108113
// Track written directories to avoid duplicates across JARs.
109114
writtenDirs := make(map[string]map[string]bool) // writer path -> set of dirs
110115

116+
for _, dir := range opts.EnsureDirs {
117+
if err := ensureParentDirs(fallback, strings.TrimSuffix(dir, "/")+"/", writtenDirs); err != nil {
118+
return fmt.Errorf("ensuring dir %s: %w", dir, err)
119+
}
120+
}
121+
111122
// Process each JAR: determine layer, write to tar, collect classpath.
112123
var classpathEntries []string
113124
usedNames := make(map[string]bool)

pkg/jarlayer/jarlayer_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,33 @@ func TestLayerData_RejectsUnsafeDestination(t *testing.T) {
622622
t.Fatalf("LayerData() error = %v, want unsafe destination error", err)
623623
}
624624
}
625+
626+
func TestLayerJars_EnsureDirs(t *testing.T) {
627+
dir := t.TempDir()
628+
629+
jar1 := filepath.Join(dir, "dep1.jar")
630+
createTestJar(t, jar1, map[string]string{
631+
"com/example/Foo.class": "foo-bytes",
632+
})
633+
634+
fallbackPath := filepath.Join(dir, "fallback.tar")
635+
636+
err := LayerJars(LayerOptions{
637+
JarPaths: []string{jar1},
638+
FallbackPath: fallbackPath,
639+
ClasspathPath: filepath.Join(dir, "classpath"),
640+
AppPrefix: "/app/lib",
641+
PathPrefix: "app/lib/",
642+
EnsureDirs: []string{"app/_main"},
643+
})
644+
if err != nil {
645+
t.Fatal(err)
646+
}
647+
648+
entries := readTar(t, fallbackPath)
649+
for _, want := range []string{"app/", "app/_main/"} {
650+
if _, ok := entries[want]; !ok {
651+
t.Errorf("expected directory entry %q in fallback tar", want)
652+
}
653+
}
654+
}

0 commit comments

Comments
 (0)