Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ZipFS): support empty archives resulting from an unlink after write in getBufferAndClose #5179

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .yarn/versions/1f698bb0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
releases:
"@yarnpkg/libzip": patch

declined:
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-patch"
- vscode-zipfs
- "@yarnpkg/cli"
- "@yarnpkg/core"
- "@yarnpkg/fslib"
- "@yarnpkg/pnp"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The following changes only affect people writing Yarn plugins:

- `yarn dlx` will no longer report false-positive `UNUSED_PACKAGE_EXTENSION` warnings
- `yarn workspace` will now set `$INIT_CWD` to the CLI working directory rather than the workspace root.
- `ZipFS.prototype.getBufferAndClose` will not error on empty archives resulting from an unlink after write.

### Shell

Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/worker-zip/index.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/yarnpkg-libzip/sources/ZipFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ export class ZipFS extends BasePortableFakeFS {
if (!this.lzSource)
throw new Error(`ZipFS was not created from a Buffer`);

// zip_source_open on an unlink-after-write empty archive fails with "Entry has been deleted"
if (this.entries.size === 0) {
this.discardAndClose();
return makeEmptyArchive();
}

try {
// Prevent close from cleaning up the source
this.libzip.source.keep(this.lzSource);
Expand Down
13 changes: 13 additions & 0 deletions packages/yarnpkg-libzip/tests/ZipFS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,19 @@ describe(`ZipFS`, () => {
expect(new ZipFS(buffer).readdirSync(PortablePath.root)).toHaveLength(0);
});

it(`should support getting the buffer from an empty in-memory zip archive (unlink after write)`, () => {
const zipFs = new ZipFS();

zipFs.writeFileSync(`/foo.txt` as PortablePath, `foo`);
zipFs.unlinkSync(`/foo.txt` as PortablePath);

const buffer = zipFs.getBufferAndClose();

expect(buffer).toStrictEqual(makeEmptyArchive());

expect(new ZipFS(buffer).readdirSync(PortablePath.root)).toHaveLength(0);
});

ifNotWin32It(`should preserve the umask`, async () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.