Skip to content

fix(init): use posix separators for the paths written into package.json - #953

Open
KallinikosMil wants to merge 1 commit into
callstack:mainfrom
KallinikosMil:fix/init-posix-separators-in-package-json
Open

fix(init): use posix separators for the paths written into package.json#953
KallinikosMil wants to merge 1 commit into
callstack:mainfrom
KallinikosMil:fix/init-posix-separators-in-package-json

Conversation

@KallinikosMil

Copy link
Copy Markdown

Summary

Running bob init on Windows writes backslashes into package.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, types and exports are module specifiers, not filesystem paths — they're always forward-slashed regardless of platform. main and types are lenient enough that some tooling still copes, but exports is 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.join into a ./… template:

entries.module = `./${path.join(output, 'module', 'index.js')}`;

path.join is correct for touching the filesystem and wrong for producing a specifier. Since these are always relative and always forward-slashed, path.posix.join is the right join — five call sites, all feeding the same entries / types objects that main, module, types and exports are 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.ts already covers this — the committed snapshot encodes the correct forward-slash output, so on Windows it fails on main today:

Before

❯ src/__tests__/init.test.ts (1 test | 1 failed)
  × initializes the configuration

  - "main": "./lib/module/index.js",
  + "main": "./lib\module\index.js",

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 lint and yarn typecheck are 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.ts still fail on my machine:

Error: EPERM: operation not permitted, symlink '…\bob-typescript-SA8Uzm' -> '…\consumer\node_modules\library'

That's a local privilege limitation, not a bug in this repo — Windows only allows fs.symlink with Developer Mode enabled or elevation, and I confirmed Developer Mode is off here (AllowDevelopmentWithoutDevLicense unset) and that a bare fs.symlinkSync fails 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 os matrices in build-local-libraries.yml and build-templates.yml are [ubuntu-latest, macos-latest], which is why this hasn't surfaced. Happy to add windows-latest in a separate PR if you'd like the coverage — I didn't want to spend your CI minutes without asking.

`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant