fix(init): use posix separators for the paths written into package.json - #953
Open
KallinikosMil wants to merge 1 commit into
Open
Conversation
`bob init` builds the values for `main`, `module`, `types` and `exports` by
interpolating `path.join` into a `./...` string. On Windows that yields
backslashes, so a freshly initialised library gets:
"main": "./lib\module\index.js",
"exports": { ".": { "default": "./lib\module\index.js" } }
Those fields are module specifiers rather than filesystem paths and are
always forward-slashed, so the manifest is wrong and `exports` in particular
will not resolve.
Join them with `path.posix` instead. The committed snapshot in init.test.ts
already encodes the correct forward-slash output and now matches on Windows
without being regenerated.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Running
bob initon Windows writes backslashes intopackage.json:{ "main": "./lib\module\index.js", "types": "./lib\typescript\src\index.d.ts", "exports": { ".": { "types": "./lib\typescript\src\index.d.ts", "default": "./lib\module\index.js" } } }main,module,typesandexportsare module specifiers, not filesystem paths — they're always forward-slashed regardless of platform.mainandtypesare lenient enough that some tooling still copes, butexportsis matched as a literal string by Node's resolver, so a library initialised on Windows ships a manifest whose entry points don't resolve.The values are built by interpolating
path.joininto a./…template:path.joinis correct for touching the filesystem and wrong for producing a specifier. Since these are always relative and always forward-slashed,path.posix.joinis the right join — five call sites, all feeding the sameentries/typesobjects thatmain,module,typesandexportsare later derived from.This is the same class of bug as callstack/react-native-paper#5054, where a platform-native path reached an import specifier.
Test plan
Windows 11, Node 22.23.2, yarn 4.11.0.
src/__tests__/init.test.tsalready covers this — the committed snapshot encodes the correct forward-slash output, so on Windows it fails onmaintoday:Before
After — passes, without the snapshot being regenerated. The only file in the diff is
init.ts; the snapshot is untouched, which is the point: the fix makes Windows produce exactly the output Linux and macOS already produce.yarn lintandyarn typecheckare both clean, and the lefthook pre-commit (eslint + tsc) passed.One thing that is not fixed here, and isn't yours
Two cases in
typescript.test.tsstill fail on my machine:That's a local privilege limitation, not a bug in this repo — Windows only allows
fs.symlinkwith Developer Mode enabled or elevation, and I confirmed Developer Mode is off here (AllowDevelopmentWithoutDevLicenseunset) and that a barefs.symlinkSyncfails the same way outside the repo entirely. I'm mentioning it only so the number of failing tests in the "before" output isn't confusing; I haven't touched those tests.Worth noting the
osmatrices inbuild-local-libraries.ymlandbuild-templates.ymlare[ubuntu-latest, macos-latest], which is why this hasn't surfaced. Happy to addwindows-latestin a separate PR if you'd like the coverage — I didn't want to spend your CI minutes without asking.