Skip to content

fix(posix): read system-scope variables back from the managed file - #72

Merged
Shinrai merged 1 commit into
nextfrom
fix/posix-system-getraw
Nov 2, 2026
Merged

Shinrai merged 1 commit into
nextfrom
fix/posix-system-getraw

Conversation

@cldmv-bot

@cldmv-bot cldmv-bot Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

🚀 What's Changed

💥 Breaking Changes

No breaking changes

✨ Features

No new features

🐛 Bug Fixes

  • fix(posix): read system-scope variables back from the managed file (a1a2230)

📦 Dependencies

No dependency updates

🔧 Other Changes

No other changes

👥 Contributors

@cldmv-bot cldmv-bot Bot added ! fix → next v4 flow: fix contributor PR targeting the next integration branch area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure labels Nov 2, 2026
@Shinrai
Shinrai merged commit 7157cbc into next Nov 2, 2026
31 checks passed
@cldmv-bot
cldmv-bot Bot deleted the fix/posix-system-getraw branch November 2, 2026 02:00
cldmv-bot Bot added a commit that referenced this pull request Nov 4, 2026
# `@cldmv/envm` v1.1.0 Changelog

**Release Date**: November 2026
**Release Type**: Minor
**Branch**: `release/1.1.0`

---

## Overview

envm moves to the CLDMV library layout. The package is now built with tsup into a single ESM bundle, `require("@cldmv/envm")` works through a small CommonJS entry that loads that same bundle, the package ships generated type declarations, and an `envm-dev` export condition lets a checkout run straight from `src/`. The `"."` and `"./cli"` entry points keep their paths and their exports.

This release also fixes four bugs that made parts of the API silently do nothing: the installed `envm` command never ran, Windows rollback never restored the registry backup, `unset` at user scope on Linux and macOS never removed the variable, and a name containing regex metacharacters could remove other variables. User scope on Linux and macOS works end to end for the first time: reading a variable there used to throw `Not implemented`, which also made every user-scope `set` and `unset` reject after it had rewritten `~/.profile`.

The package now declares `engines.node` `>=14.14.0`. Earlier releases declared no floor at all; see [Requirements](#-requirements).

---

## 📦 Requirements

### Node.js `>=14.14.0`, with `require()` on `^20.19.0` or `>=22.13.0` ([#67](#67))

v1.0.12 and earlier had no `engines` field. v1.1.0 sets `engines.node` to `>=14.14.0`, the first version with the `fs/promises` `rm()` that the Windows registry helper uses. `import` works from there. `require()` loads the ESM build through Node's `require(esm)`, so it needs Node.js `^20.19.0` or `>=22.13.0`; on an older Node.js the CommonJS entry throws `ERR_REQUIRE_ESM` naming that range and pointing at `import()`.

Developing the package needs Node.js 22.22.0 or later, because the build toolchain (`tsup` 9 and its `chokidar` 6 dependency) doesn't run on anything older. That floor is declared in npm's `devEngines` (`runtime` `node` `>=22.22.0`, `onFail: "error"`), so npm enforces it in a checkout of this repository and never for consumers installing the package.

---

## ✨ Features

### CLDMV library layout with a CommonJS entry ([#60](#60), closes [#59](#59))

- **tsup build.** The old `esbuild` command wrote one unbundled file per source module. tsup now builds `dist/index.mjs` (the library, with the platform adapters and backup helpers bundled in) and `dist/cli.mjs` (the CLI, which keeps its `./index.mjs` import external so it loads the library bundle instead of carrying a second copy). Output is minified with function names kept. The library plus CLI is about 15 KB minified, down from about 29 KB.
- **`require()` support.** `src/index.cjs` is a short wrapper that `require()`s the sibling `index.mjs` through Node's `require(esm)` and re-exports `{ envManager }`. It is copied verbatim to `dist/index.cjs`, so CommonJS and ESM callers share one copy of the library and its state. On a Node.js version without `require(esm)` it throws `ERR_REQUIRE_ESM` with a message naming the supported range and pointing at `import()`.
- **Type declarations.** `types/` is generated from the JSDoc by `tsc` and published; `exports` carries a `types` condition for both entry points. `envManager` is typed through explicit `EnvManager`, `PathHelpers` and `BackupHelpers` typedefs so its declaration emits as a typed `const` ([#63](#63)).
- **`envm-dev` export condition.** `"."` and `"./cli"` resolve to `dist/` by default. In a checkout, `node --conditions=envm-dev` resolves them to `src/` instead. The Vitest suite runs against `src/` through the condition, and `npm run test:dist` builds the package and checks the built entries with `node --test`.
- **Shared backup state in the CLI.** The CLI's exit-time purge now goes through `envManager.backup`, so it uses the same backup directory as the library rather than a separately loaded copy of `backup.mjs`.
- `package.json` gains `main` (`./dist/index.cjs`), `module`, `types`, a `./package.json` export, and a `prepack` build in place of `prepublishOnly`.

---

## 🐛 Bug Fixes

### The installed `envm` command did nothing ([#60](#60), fixes [#61](#61))

`src/cli.mjs` only ran `main()` when `import.meta.url` equalled `` `file://${process.argv[1]}` ``. An installed `envm` runs through the `node_modules/.bin/envm` symlink, so `process.argv[1]` was the link path, the check failed and the command exited without doing anything. Paths containing URL-encoded characters such as spaces failed the same way. The check now compares `fileURLToPath(import.meta.url)` with `realpathSync(process.argv[1])`. The CLI file also starts with a `#!/usr/bin/env node` line, which the published bin lacked.

### Windows rollback never restored the registry backup ([#60](#60), fixes [#62](#62))

When verification failed after a `set` or `unset` at user or system scope, the rollback path called `require("os")` and `require("fs")`. `require` is not defined in an ES module, so it threw a `ReferenceError`, the surrounding `catch` swallowed it, and the registry backup was never imported. Rollback now uses the module's own imports and works.

### `unset` at user scope never removed the variable on Linux and macOS ([#60](#60))

The filter for `~/.profile` lines was a `RegExp` built from a template literal, where `\s` lost its backslash and the pattern became `^exports+NAME=`. It never matched, so the `export` line stayed in the envm block and the variable survived the `unset`. The filter now parses each `export NAME=` line and compares the name literally, which also fixes the next item.

### User-scope variables on Linux and macOS couldn't be read ([#69](#69), fixes [#68](#68))

`getRaw()` and `getExpanded()` at user scope on POSIX were stubs that threw `Not implemented: getRaw user`. `set` and `unset` at user scope verify the change by reading the variable back, so both rewrote `~/.profile` and then rejected with that error, and the rollback that should follow a failed verification never ran. `getRaw()` now reads the value from the managed `# envm-begin` / `# envm-end` block in `~/.profile`, unquoting the `'value'` envm writes, and returns `undefined` when the profile, the block or the variable is missing. `set` uses the same line parser, so it updates an existing line in place for names that aren't plain word characters (`A.B`) instead of adding a second one.

### System-scope variables on Linux and macOS couldn't be read ([#72](#72), fixes [#71](#71))

`getRaw` for the system scope was a stub that threw, so every system-scope `set` and `unset` failed at its verify step after writing `/etc/environment`. It now reads the value back from that file, matching the name exactly. `ENVM_SYSTEM_ENV_FILE` points envm at another file, which the tests use.

### Variable names were treated as regex patterns ([#60](#60))

Because that filter interpolated the variable name into a `RegExp`, metacharacters in the name matched other variables: unsetting `A.B` would also remove `AxB`. Names are now compared as plain strings.

### Registry import files went to a predictable temp path ([#60](#60))

Windows rollback and `backup.restore()` wrote the `.reg` file to a fixed-pattern name in the shared temp directory, and `restore()` never deleted it. All three paths now go through one helper that creates a private directory with `mkdtemp`, writes the file with exclusive create and mode `0600`, runs `reg import`, and removes the directory afterwards.

---

## 🔧 CI & tooling

- **CI matrix floor raised to 22.22.0** ([#60](#60), [#67](#67)). The dev toolchain needs Node.js 22.22, so the CI and publish matrices start there (previously 22.12.0). Their `min_node_version` descriptions say it is the dev toolchain floor and should match `devEngines`, not `engines`.
- tsup builds for `node14`, matching `engines.node` ([#67](#67)).
- `build:ci` now runs lint, the format check, the declaration build and the tsup build; the CI coverage legs run `build:ci`, and the bundle-size check measures the `.mjs` and `.cjs` files in `dist/`.
- ESLint and Prettier configs moved to `.configs/` on the shared CLDMV base, with `lint`, `lint:fix`, `format` and `format:check` scripts and a `.prettierignore`; the existing lint errors were cleared. The leftover `jest.config.mjs` is gone.
- TypeScript is pinned to 6 for the declaration build, because TypeScript 7's JavaScript declaration emit isn't complete yet, and the declarations were regenerated with it ([#63](#63)).

---

## 📚 Documentation

- **NEW:** [docs/changelog/v1/v1.1.0.md](./v1.1.0.md) — this changelog.
- README: the Node.js requirements for `import`, `require()` and development ([#67](#67)), a **CommonJS** usage section, a **Development** section on the `envm-dev` condition and the build, and **What's New** now leads with v1.1.0.

---

## 🔧 Dependencies

The package still has no runtime dependencies. Everything below is development-only.

- **Security overrides** ([#63](#63)): `katex` `>=0.19.1 <1.0.0` and `minimatch` `^11.0.3`, clearing dev-only advisories that come in through `eslint` and `@eslint/markdown`. Neither package is installed by consumers of `envm`.
- **Toolchain bumps** ([#63](#63)): `@cldmv/configs` 1.2.4 → 1.3.0, `@cldmv/fix-headers` 2.2.0 → 2.2.4, `@cldmv/vitest-runner` 1.5.3 → 1.5.6, `vitest` and `@vitest/coverage-v8` 5.0.3 → 5.0.5. The lockfile now resolves `vite` 8.3.5 and `source-map-js` 1.2.2, clearing the open Dependabot alerts for both and superseding [#49](#49), [#52](#52), [#54](#54), [#56](#56) and [#58](#58).
- **Added for the new build and lint setup** ([#60](#60)): `tsup` 9.0.0, `typescript` 6.0.3, `@types/node`, `eslint` 10.14.0, `prettier` 3.9.10, `@eslint/js`, `@eslint/json`, `@eslint/markdown`, `@eslint/css`, `globals`, `@cldmv/jsonv`, `@cldmv/eslint-plugin-jsonv` and `@cldmv/prettier-plugin-jsonv`.
- **Removed:** the direct `esbuild` devDependency (tsup brings its own).

---

## Upgrade notes

- `engines.node` is now `>=14.14.0`; v1.0.12 and earlier declared no `engines` field. `require()` needs Node.js `^20.19.0` or `>=22.13.0`. CI only tests Node.js 22.22.0 and later.
- `import { envManager } from "@cldmv/envm"` and the `envm` command work as before. `require("@cldmv/envm")` now works too and returns the same `envManager`.
- The installed `envm` command now actually runs, so scripts that called it and silently got no effect will start changing environment variables. Windows rollback and user-scope `unset` on Linux and macOS now do what they always claimed to, and user-scope `set`, `unset`, `getRaw` and `getExpanded` on Linux and macOS resolve instead of throwing `Not implemented: getRaw user`.



<details>
<summary>👥 Contributors</summary>

- @Shinrai

</details>

---

<!-- coverage-start -->

![coverage](https://img.shields.io/badge/coverage-62.2%25-yellow?style=for-the-badge&logo=vitest&logoColor=white)

| Metric | Coverage |
|--------|----------|
| Statements | 66.3% |
| Branches   | 50.0% |
| Functions  | 64.7% |
| Lines      | 68.0% |

*Avg: **62.2%** · `78b80df` · Node lts/**

<!-- coverage-end -->

<!-- co-authors -->

Co-authored-by: Shinrai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure ! fix → next v4 flow: fix contributor PR targeting the next integration branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant