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: shim non-existent source #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

trappar
Copy link

@trappar trappar commented Feb 25, 2023

PNPM has this long-standing issue where pnpm install fails when a package defines a bin that must be compiled first: pnpm/pnpm#1801

In this case the file can't be read and so a shebang can't be determined, and execution fails with error code ENOENT. This seems easily solvable. cmd-shim can still fall back to determining the program by using the extensionToProgramMap.

This PR is probably incomplete. Not being terribly familiar with the code, I've done my best, but the test is probably incomplete at the very least.

@zkochan
Copy link
Member

zkochan commented Mar 15, 2023

I guess we can use this approach.

@trappar
Copy link
Author

trappar commented Apr 2, 2023

I've rebased the PR on the recent changes to main. Not sure what's going on with the broken tests. Seems like the checks run on Windows, so I tried running it locally. I've run it on Windows 11 with both Node 14 and 18 and all tests pass for me. Any chance you might be willing to help push this across the finish line? Probably a lot easier for you given that you're more familiar with the code 😅

Currently I'm working around this by adding the following to my monorepo's root package.json

"scripts": {
  "pnpm:devPreinstall": "scripts/createBinStubs"
},

Which runs this script:

#!/usr/bin/env node
const { writeFileSync, mkdirSync, existsSync } = require("fs");
const { dirname } = require("path");

const shims = [
  "packages/[redacted]/dist/index.js",
];

const fileContent = `/*
 * Temp file generated by scripts/createBinStubs.js
 * This file is created as a temporary workaround for this issue: https://github.com/pnpm/pnpm/issues/1801
 * Currently PNPM fails to create a bin while installing sub-packages when the source for that bin is missing while running pnpm install.
 * This is the case for any package with a bin that is built after install. By creating this temporary file, PNPM is able to link to it.
 * Once the actual bin is built, everything works.
*/`;

shims.forEach((shim) => {
  const shimDir = dirname(shim);
  if (!existsSync(shimDir)) {
    mkdirSync(shimDir, { recursive: true });
  }
  if (!existsSync(shim)) {
    writeFileSync(shim, fileContent);
  }
});

I'd love to remove this hacky workaround from my code!

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.

None yet

2 participants