Do not package empty directories (#330)

Changes the packager to exclude any empty directories.

This change means that pkl project package for an already published packages will fail. The packager checks for an existing package at this version, and compares checksums. It will then error if the checksum has changed.

This is technically a breaking change, albeit a minor one. The workaround is to publish new versions of packages.

Published packages should still be compatible with Pkl 0.25.
This commit is contained in:
Stefan M
2024-03-19 17:34:47 +01:00
committed by GitHub
parent 5d0d2ce7a1
commit 46d65506d5
2 changed files with 22 additions and 30 deletions

View File

@@ -241,7 +241,7 @@ class CliProjectPackagerTest {
"type": "remote", "type": "remote",
"uri": "projectpackage://localhost:12110/birds@0.5.0", "uri": "projectpackage://localhost:12110/birds@0.5.0",
"checksums": { "checksums": {
"sha256": "3f19ab9fcee2f44f93a75a09e531db278c6d2cd25206836c8c2c4071cd7d3118" "sha256": "04eec465b217fb9779489525d26e9b587e5e47ff4d584c7673a450109715bc31"
} }
}, },
"package://localhost:12110/fruit@1": { "package://localhost:12110/fruit@1": {
@@ -332,7 +332,7 @@ class CliProjectPackagerTest {
"version": "1.0.0", "version": "1.0.0",
"packageZipUrl": "https://foo.com", "packageZipUrl": "https://foo.com",
"packageZipChecksums": { "packageZipChecksums": {
"sha256": "7f515fbc4b229ba171fac78c7c3f2c2e68e2afebae8cfba042b12733943a2813" "sha256": "e83b67722ea17ba41717ce6e99ae8ee02d66df6294bd319ce403075b1071c3e0"
}, },
"dependencies": {}, "dependencies": {},
"authors": [] "authors": []
@@ -344,9 +344,9 @@ class CliProjectPackagerTest {
assertThat(expectedArchive.zipFilePaths()) assertThat(expectedArchive.zipFilePaths())
.hasSameElementsAs(listOf("/", "/c", "/c/d", "/c/d/foo.txt", "/a", "/a/b", "/a/b/foo.pkl")) .hasSameElementsAs(listOf("/", "/c", "/c/d", "/c/d/foo.txt", "/a", "/a/b", "/a/b/foo.pkl"))
assertThat(expectedMetadataChecksum) assertThat(expectedMetadataChecksum)
.hasContent("203ef387f217a3caee7f19819ef2b50926929269241cb7b3a29d95237b2c7f4b") .hasContent("72ab32b27393bde5f316b00f184faae919378e4d7643872c605f681b14b647bf")
assertThat(expectedArchiveChecksum) assertThat(expectedArchiveChecksum)
.hasContent("7f515fbc4b229ba171fac78c7c3f2c2e68e2afebae8cfba042b12733943a2813") .hasContent("e83b67722ea17ba41717ce6e99ae8ee02d66df6294bd319ce403075b1071c3e0")
FileSystems.newFileSystem(URI("jar:" + expectedArchive.toUri()), mutableMapOf<String, String>()) FileSystems.newFileSystem(URI("jar:" + expectedArchive.toUri()), mutableMapOf<String, String>())
.use { fs -> .use { fs ->
assertThat(fs.getPath("a/b/foo.pkl")).hasSameTextualContentAs(fooPkl) assertThat(fs.getPath("a/b/foo.pkl")).hasSameTextualContentAs(fooPkl)
@@ -364,6 +364,10 @@ class CliProjectPackagerTest {
writeEmptyFile("main.test.pkl") writeEmptyFile("main.test.pkl")
writeEmptyFile("child/main.pkl") writeEmptyFile("child/main.pkl")
writeEmptyFile("child/main.test.pkl") writeEmptyFile("child/main.test.pkl")
writeEmptyFile("examples/Workflow.pkl")
writeEmptyFile("examples/Ex1.pkl")
writeEmptyFile("tests/Test1.pkl")
writeEmptyFile("tests/integration/TestIng1.pkl")
} }
tempDir.writeFile( tempDir.writeFile(
@@ -380,6 +384,8 @@ class CliProjectPackagerTest {
"*.bin" "*.bin"
"child/main.pkl" "child/main.pkl"
"*.test.pkl" "*.test.pkl"
"examples/Ex1.pkl"
"tests/**"
} }
} }
""" """
@@ -399,14 +405,12 @@ class CliProjectPackagerTest {
.hasSameElementsAs( .hasSameElementsAs(
listOf( listOf(
"/", "/",
"/a", "/examples",
"/a/b", "/examples/Workflow.pkl",
"/a/b/c",
"/child",
"/input", "/input",
"/input/foo", "/input/foo",
"/input/foo/bar.txt", "/input/foo/bar.txt",
"/main.pkl", "/main.pkl"
) )
) )
} }
@@ -505,7 +509,7 @@ class CliProjectPackagerTest {
"version": "1.0.0", "version": "1.0.0",
"packageZipUrl": "https://foo.com", "packageZipUrl": "https://foo.com",
"packageZipChecksums": { "packageZipChecksums": {
"sha256": "7d08a65078e0bfc382c16fe1bb94546ab9a11e6f551087f362a4515ca98102fc" "sha256": "8739c76e681f900923b900c9df0ef75cf421d39cabb54650c4b9ad19b6a76d85"
}, },
"dependencies": { "dependencies": {
"birds": { "birds": {
@@ -517,7 +521,7 @@ class CliProjectPackagerTest {
"project2": { "project2": {
"uri": "package://localhost:12110/project2@5.0.0", "uri": "package://localhost:12110/project2@5.0.0",
"checksums": { "checksums": {
"sha256": "ddebb806e5b218ebb1a2baa14ad206b46e7a0c1585fa9863a486c75592bc8b16" "sha256": "6f469b28f8b62a8a1191e2749bcf9c27dedbbb1e0ea754ac34af57b534e0ddda"
} }
} }
}, },
@@ -537,7 +541,7 @@ class CliProjectPackagerTest {
"version": "5.0.0", "version": "5.0.0",
"packageZipUrl": "https://foo.com/project2.zip", "packageZipUrl": "https://foo.com/project2.zip",
"packageZipChecksums": { "packageZipChecksums": {
"sha256": "7d08a65078e0bfc382c16fe1bb94546ab9a11e6f551087f362a4515ca98102fc" "sha256": "8739c76e681f900923b900c9df0ef75cf421d39cabb54650c4b9ad19b6a76d85"
}, },
"dependencies": {}, "dependencies": {},
"authors": [] "authors": []
@@ -914,7 +918,7 @@ class CliProjectPackagerTest {
""" """
Package `package://localhost:12110/birds@0.5.0` was already published with different contents. Package `package://localhost:12110/birds@0.5.0` was already published with different contents.
Computed checksum: 7324e17214b6dcda63ebfb57d5a29b077af785c13bed0dc22b5138628a3f8d8f Computed checksum: 04eec465b217fb9779489525d26e9b587e5e47ff4d584c7673a450109715bc31
Published checksum: 0a5ad2dc13f06f73f96ba94e8d01d48252bc934e2de71a837620ca0fef8a7453 Published checksum: 0a5ad2dc13f06f73f96ba94e8d01d48252bc934e2de71a837620ca0fef8a7453
""" """
.trimIndent() .trimIndent()

View File

@@ -278,13 +278,6 @@ public class ProjectPackager {
} }
} }
private String ensureEndsWithSlash(Path file) {
if (file.endsWith("/")) {
return file.toString();
}
return file + "/";
}
/** /**
* Sets mtime to 0 so package creation is idempotent. Running the packager multiple times produces * Sets mtime to 0 so package creation is idempotent. Running the packager multiple times produces
* the same output. * the same output.
@@ -302,16 +295,10 @@ public class ProjectPackager {
try (var zos = new ZipOutputStream(digestOutputStream)) { try (var zos = new ZipOutputStream(digestOutputStream)) {
for (var file : files) { for (var file : files) {
var relativePath = project.getProjectDir().relativize(file); var relativePath = project.getProjectDir().relativize(file);
if (Files.isDirectory(file)) { var zipEntry = new ZipEntry(relativePath.toString());
var zipEntry = new ZipEntry(ensureEndsWithSlash(relativePath)); zipEntry.setTime(ZIP_ENTRY_MTIME);
zipEntry.setTime(ZIP_ENTRY_MTIME); zos.putNextEntry(zipEntry);
zos.putNextEntry(zipEntry); Files.copy(file, zos);
} else {
var zipEntry = new ZipEntry(relativePath.toString());
zipEntry.setTime(ZIP_ENTRY_MTIME);
zos.putNextEntry(zipEntry);
Files.copy(file, zos);
}
zos.closeEntry(); zos.closeEntry();
} }
} catch (IOException e) { } catch (IOException e) {
@@ -344,6 +331,7 @@ public class ProjectPackager {
var excludePatterns = getExcludePatterns(pkg); var excludePatterns = getExcludePatterns(pkg);
try (var stream = Files.walk(project.getProjectDir())) { try (var stream = Files.walk(project.getProjectDir())) {
return stream return stream
.filter(Files::isRegularFile)
.filter( .filter(
(it) -> { (it) -> {
var fileNameRelativeToProjectRoot = var fileNameRelativeToProjectRoot =