-
Notifications
You must be signed in to change notification settings - Fork 5k
pack, publish: include entries on filesystems whose readdir reports DT_UNKNOWN #38716
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
Open
robobun
wants to merge
8
commits into
main
Choose a base branch
from
farm/58df7f14/pack-dt-unknown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9478e50
pack: resolve directory entries whose readdir type is unknown
robobun c9c9c24
sys: add IteratorResult::resolve_kind, use it from pack and publish
robobun d635214
sys: shorten the resolve_kind comments
robobun 093ab3f
sys: tighten the resolve_kind doc comment
robobun a02e49a
sys: resolve unknown entry types inside the directory iterator
robobun 3e3eb93
sys: shorter comments on the resolve flag
robobun 8166de6
test: compile the DT_UNKNOWN shim in beforeAll, not inside the tests
robobun 2c6552f
Leave walker_skippable and prune to the follow-up sweep
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // Some filesystems (FUSE, NFS, XFS formatted with ftype=0) do not fill in | ||
| // d_type, so every readdir entry comes back as DT_UNKNOWN. The package manager | ||
| // commands must behave as they do elsewhere; `dtUnknownReaddir` (harness) | ||
| // simulates such a filesystem with an LD_PRELOAD shim. | ||
| import { readTarball } from "bun:internal-for-testing"; | ||
| import { beforeAll, describe, expect, test } from "bun:test"; | ||
| import { bunExe, dtUnknownReaddir, tempDir } from "harness"; | ||
| import { symlink, writeFile } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
|
|
||
| let env: NodeJS.Dict<string>; | ||
|
|
||
| // Compiles the shim; a C compiler on a busy CI machine can take longer than the | ||
| // default hook timeout. | ||
| beforeAll(async () => { | ||
| if (dtUnknownReaddir.available) env = await dtUnknownReaddir.env(); | ||
| }, 30_000); | ||
|
|
||
| async function run(cwd: string, ...args: string[]) { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), ...args], | ||
| cwd, | ||
| env, | ||
| stdin: "ignore", | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toContain(dtUnknownReaddir.marker); | ||
| expect({ stdout, stderr, exitCode }).toMatchObject({ stderr: expect.not.stringContaining("error:"), exitCode: 0 }); | ||
| } | ||
|
|
||
| function packedPaths(tarball: string): string[] { | ||
| return readTarball(tarball) | ||
| .entries.map((entry: { pathname: string }) => entry.pathname) | ||
| .sort(); | ||
| } | ||
|
|
||
| describe.skipIf(!dtUnknownReaddir.available)("pack on a filesystem whose readdir reports DT_UNKNOWN", () => { | ||
| test.concurrent("packs the project tree", async () => { | ||
| using dir = tempDir("dt-unknown-tree", { | ||
| "package.json": JSON.stringify({ name: "dt-unknown-tree", version: "1.0.0" }), | ||
| "index.js": "", | ||
| "lib/a.js": "", | ||
| "lib/nested/b.js": "", | ||
| // `out/` only ignores directories, so it needs the entry's kind: the | ||
| // `out` directory is ignored, the `lib/out` file is not. | ||
| ".npmignore": "out/\n", | ||
| "out/c.js": "", | ||
| "lib/out": "", | ||
| }); | ||
| // Symlinks are never packed; resolving the kind with lstat has to keep that. | ||
| await symlink("index.js", join(String(dir), "link.js")); | ||
|
|
||
| await run(String(dir), "pm", "pack"); | ||
|
|
||
| expect(packedPaths(join(String(dir), "dt-unknown-tree-1.0.0.tgz"))).toEqual([ | ||
| "package/index.js", | ||
| "package/lib/a.js", | ||
| "package/lib/nested/b.js", | ||
| "package/lib/out", | ||
| "package/package.json", | ||
| ]); | ||
| }); | ||
|
|
||
| test.concurrent('packs what "files" selects', async () => { | ||
| using dir = tempDir("dt-unknown-files", { | ||
| "package.json": JSON.stringify({ | ||
| name: "dt-unknown-files", | ||
| version: "1.0.0", | ||
| files: ["index.js", "lib", "!lib/internal/"], | ||
| }), | ||
| "index.js": "", | ||
| "excluded.js": "", | ||
| "lib/a.js": "", | ||
| "lib/nested/b.js": "", | ||
| "lib/internal/c.js": "", | ||
| }); | ||
| await symlink("a.js", join(String(dir), "lib", "link.js")); | ||
|
|
||
| await run(String(dir), "pm", "pack"); | ||
|
|
||
| expect(packedPaths(join(String(dir), "dt-unknown-files-1.0.0.tgz"))).toEqual([ | ||
| "package/index.js", | ||
| "package/lib/a.js", | ||
| "package/lib/nested/b.js", | ||
| "package/package.json", | ||
| ]); | ||
| }); | ||
|
|
||
| test.concurrent("packs bundledDependencies", async () => { | ||
| using dir = tempDir("dt-unknown-bundled", { | ||
| "package.json": JSON.stringify({ | ||
| name: "dt-unknown-bundled", | ||
| version: "1.0.0", | ||
| dependencies: { "dep": "1.0.0", "@scope/dep": "1.0.0", "not-bundled": "1.0.0" }, | ||
| bundledDependencies: ["dep", "@scope/dep"], | ||
| }), | ||
| "index.js": "", | ||
| "node_modules/dep/package.json": JSON.stringify({ name: "dep", version: "1.0.0" }), | ||
| "node_modules/dep/lib/index.js": "", | ||
| "node_modules/@scope/dep/package.json": JSON.stringify({ name: "@scope/dep", version: "1.0.0" }), | ||
| "node_modules/@scope/dep/index.js": "", | ||
| "node_modules/not-bundled/package.json": JSON.stringify({ name: "not-bundled", version: "1.0.0" }), | ||
| }); | ||
|
|
||
| await run(String(dir), "pm", "pack"); | ||
|
|
||
| expect(packedPaths(join(String(dir), "dt-unknown-bundled-1.0.0.tgz"))).toEqual([ | ||
| "package/index.js", | ||
| "package/node_modules/@scope/dep/index.js", | ||
| "package/node_modules/@scope/dep/package.json", | ||
| "package/node_modules/dep/lib/index.js", | ||
| "package/node_modules/dep/package.json", | ||
| "package/package.json", | ||
| ]); | ||
| }); | ||
|
|
||
| test.concurrent('publish packs the tree, walks "directories.bin" and finds the readme', async () => { | ||
| let captured: any; | ||
| using registry = Bun.serve({ | ||
| port: 0, | ||
| async fetch(req) { | ||
| if (req.method === "PUT") captured = await req.json(); | ||
| return new Response("OK"); | ||
| }, | ||
| }); | ||
| using dir = tempDir("dt-unknown-publish", { | ||
| "bunfig.toml": `[install]\ncache = false\nregistry = { url = "http://localhost:${registry.port}", token = "unused" }\n`, | ||
| "package.json": JSON.stringify({ | ||
| name: "dt-unknown-publish", | ||
| version: "1.0.0", | ||
| directories: { bin: "bins" }, | ||
| }), | ||
| "README.md": "# dt-unknown-publish", | ||
| "index.js": "", | ||
| "bins/a.js": "", | ||
| "bins/more/b.js": "", | ||
| }); | ||
|
|
||
| await run(String(dir), "publish"); | ||
|
|
||
| expect(captured.versions["1.0.0"]).toMatchObject({ | ||
| bin: { "a.js": "bins/a.js", "more": "bins/more", "b.js": "bins/more/b.js" }, | ||
| readme: "# dt-unknown-publish", | ||
| readmeFilename: "README.md", | ||
| }); | ||
|
|
||
| const attachment: { data: string } = Object.values(captured._attachments)[0] as any; | ||
| const tarball = join(String(dir), "published.tgz"); | ||
| await writeFile(tarball, Buffer.from(attachment.data, "base64")); | ||
| expect(packedPaths(tarball)).toEqual([ | ||
| "package/README.md", | ||
| "package/bins/a.js", | ||
| "package/bins/more/b.js", | ||
| "package/index.js", | ||
| "package/package.json", | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // LD_PRELOAD shim: every getdents64 record comes back with d_type == DT_UNKNOWN, | ||
| // the way FUSE, some NFS servers and XFS formatted with ftype=0 report entries. | ||
| // bun issues getdents64 through libc's syscall() wrapper, which this interposes. | ||
| // Compiled by `dtUnknownReaddir` in test/harness.ts, which defines MARKER: it is | ||
| // written to stderr the first time a record is rewritten so a test can tell the | ||
| // shim actually saw bun's readdir calls. | ||
| #define _GNU_SOURCE | ||
| #include <dlfcn.h> | ||
| #include <stdarg.h> | ||
| #include <stdint.h> | ||
| #include <string.h> | ||
| #include <sys/syscall.h> | ||
| #include <unistd.h> | ||
|
|
||
| static long (*real_syscall)(long, long, long, long, long, long, long); | ||
| static int announced; | ||
|
|
||
| long syscall(long number, ...) { | ||
| va_list ap; | ||
| long a, b, c, d, e, f; | ||
| va_start(ap, number); | ||
| a = va_arg(ap, long); | ||
| b = va_arg(ap, long); | ||
| c = va_arg(ap, long); | ||
| d = va_arg(ap, long); | ||
| e = va_arg(ap, long); | ||
| f = va_arg(ap, long); | ||
| va_end(ap); | ||
| if (!real_syscall) { | ||
| real_syscall = (long (*)(long, long, long, long, long, long, long))dlsym(RTLD_NEXT, "syscall"); | ||
| } | ||
| long rc = real_syscall(number, a, b, c, d, e, f); | ||
| if (number != SYS_getdents64 || rc <= 0) return rc; | ||
| if (!announced) { | ||
| announced = 1; | ||
| static const char marker[] = MARKER "\n"; | ||
| if (write(2, marker, sizeof(marker) - 1) < 0) { | ||
| } | ||
| } | ||
| // struct linux_dirent64 { u64 d_ino; s64 d_off; u16 d_reclen; u8 d_type; char d_name[]; } | ||
| unsigned char *buf = (unsigned char *)b; | ||
| for (long off = 0; off + 19 <= rc;) { | ||
| uint16_t reclen; | ||
| memcpy(&reclen, buf + off + 16, sizeof(reclen)); | ||
| if (reclen == 0) break; | ||
| buf[off + 18] = 0; /* DT_UNKNOWN */ | ||
| off += reclen; | ||
| } | ||
| return rc; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.