Skip to content

Report the package's own version and smoke-test the packed package - #107

Merged
scalvert merged 2 commits into
mainfrom
fix/version-and-pack
Oct 10, 2026
Merged

scalvert merged 2 commits into
mainfrom
fix/version-and-pack

Conversation

@scalvert

@scalvert scalvert commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Summary

md-code --version prints the wrong version when markdown-code is installed as a project dependency or run through npx, including the published 1.2.0:

Install Prints
npm install markdown-code your project's own version
npx markdown-code unknown
npm install -g markdown-code correct (yargs is nested under our package)

cli.ts called yargs' .version() with no argument. yargs then guesses the version by looking for package.json above the first node_modules in yargs' install path. In this repo, that's our package.json, so the tests passed. Installed as a dependency, it's the consumer's project. Under npx, it finds no package.json.

Changes

  • cli.ts: .version(VERSION). src/version.ts already reads the package's own package.json relative to dist/.
  • scripts/smoke-test-package.mjs (npm run test:package, run after npm run build):
    • packs the package and checks the tarball has only dist/, LICENSE, README.md, package.json and the logo, including dist/index.d.ts;
    • installs it into a temp project and checks md-code --version and markdown-code --version print the package version;
    • runs sync and check on a small project;
    • imports the library and checks syncMarkdownFiles and VERSION.
  • CI runs it after the tests. test:all includes it, and it's documented in CONTRIBUTING.md and AGENTS.md.

The script runs npm as node $npm_execpath with no shell, so paths with spaces work on Windows too, ready for the cross-platform CI matrix in the next PR. It stops with a hint if it isn't run through npm or if dist/ is missing.

Tests

Against the unfixed CLI, the smoke test fails with '0.0.0' !== '1.2.0'; 0.0.0 is the temp project's own version. With the fix, it passes. The in-repo --version test can't catch this, because inside the repo yargs finds the right package.json.

  • I have run npm run lint and fixed any issues.
  • I have run npm run build to verify the project builds.
  • I have run npm test to ensure all tests pass. (317 passed, plus npm run test:package)

yargs' argument-less .version() guesses from the package.json above its own node_modules, so an installed md-code printed the consumer project's version or 'unknown'. Pass VERSION explicitly. Add scripts/smoke-test-package.mjs (npm run test:package), which packs, installs into a temp project, and checks the bins, sync/check, the library import, and the tarball contents; run it in CI.
@scalvert scalvert added the bug Something isn't working label Oct 10, 2026
Use node with npm_execpath so Windows paths with spaces work without shell quoting, and stop with a hint when not run through npm or when dist/ is missing.
@scalvert

Copy link
Copy Markdown
Owner Author

Second-pass review

Root cause confirmed. yargs 18's ESM shim sets mainFilename to its own path, cut at the last node_modules, and .version() looks upward from there. Tested with Node 22.16 and npm 10.9.2:

Install Before After
local dependency (consumer at 9.9.9) 9.9.9 1.2.0
npx --package=<tgz> unknown 1.2.0
npm i -g --prefix 1.2.0 1.2.0

version.ts is bundled only into dist/cli.js and dist/index.js, and both resolve dist/../package.json.

Fixed in c2fa887

  • The description claimed global installs were broken; they never were. It now has the table above.
  • The smoke test runs npm as node $npm_execpath instead of npm.cmd with shell: true. Windows paths with spaces would have broken unquoted shell args, and Node 24 warns (DEP0190) about args combined with shell: true.
  • It stops with a hint when not run through npm or when dist/ is missing. Previously a missing build surfaced only as "package is missing dist/index.d.ts".

Noted, not changed

  • A stale dist/ is tested as-is, because npm pack doesn't build (there's no prepack). CI always builds first.
  • scripts/ isn't linted. The next PR lints beyond src/.

CI green on Node 22 and 24, including the new smoke step.

@scalvert
scalvert merged commit c203c72 into main Oct 10, 2026
2 checks passed
@scalvert
scalvert deleted the fix/version-and-pack branch October 10, 2026 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant