GoogleContainerTools / GoogleContainerTools/jib

NPE when adding LayerEntry relative to the root '/'

Open
#2,195 4 comments 2 reactions 0 assignees View on GitHub
area/jib-core priority:p3 type:bug
Dominant language
Java
Stars
14.5k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

**Environment**:

- *Jib version:* jib-core 0.12.0
- *Build tool:* cram
- *OS:* macOS

**Description of the issue**:

Seeing NPEs when using jib-core to create a layer with content placed at "/":
```
Caused by: java.lang.NullPointerException
at com.google.cloud.tools.jib.image.ReproducibleLayerBuilder$UniqueTarArchiveEntries.add(ReproducibleLayerBuilder.java:75)
at com.google.cloud.tools.jib.image.ReproducibleLayerBuilder$UniqueTarArchiveEntries.access$100(ReproducibleLayerBuilder.java:47)
at com.google.cloud.tools.jib.image.ReproducibleLayerBuilder.build(ReproducibleLayerBuilder.java:118)
at com.google.cloud.tools.jib.builder.steps.BuildAndCacheApplicationLayerStep.call(BuildAndCacheApplicationLayerStep.java:107)
at com.google.cloud.tools.jib.builder.steps.BuildAndCacheApplicationLayerStep.call(BuildAndCacheApplicationLayerStep.java:37)
... 6 more
```

The code in question:
https://github.com/GoogleContainerTools/jib/blob/a692b8b0cde1f982f6e28309dd08ca80460950e2/jib-core/src/main/java/com/google/cloud/tools/jib/image/ReproducibleLayerBuilder.java#L73-L76

The layer configurations had been built with `LayerConfiguration.Builder.addEntryRecursive()`

In this case, `tarEntryPath.getName()` is `/` (the root directory). `namePath` is a `sun.nio.fs.UnixPath` on "/" with filesystem of type `sun.nio.fs.MacOSXFileSystem`. But `namePath.getParent()` returns `null`, which adheres to the contract of `Path#getParent()`: _Returns the parent path, or `null` if this path does not have a parent._

So an obvious fix is to change this guard to
```java
if (namePath.getParent() == null || namePath.getParent() != namePath.getRoot()) {
```

But should we be adding a tar entry for the root directory?

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.