@smoothbricks/nx-plugin 0.4.27 → 0.4.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,7 @@ import {
13
13
  readdirSync,
14
14
  readFileSync,
15
15
  readlinkSync,
16
+ readSync,
16
17
  realpathSync,
17
18
  renameSync,
18
19
  rmSync,
@@ -1203,6 +1204,178 @@ function bunInstaller(inputs: readonly string[]): Installer {
1203
1204
  * in the checkout is part of the identity: the relative `.pth` lines hold only
1204
1205
  * there.
1205
1206
  */
1207
+ function sitePackagesDirectories(environment: string): string[] {
1208
+ const lib = path.join(environment, 'lib');
1209
+ const directories: string[] = [];
1210
+ for (const python of existsSync(lib) ? readdirSync(lib) : []) {
1211
+ if (!python.startsWith('python')) continue;
1212
+ const sitePackages = path.join(lib, python, 'site-packages');
1213
+ if (existsSync(sitePackages)) directories.push(sitePackages);
1214
+ }
1215
+ return directories;
1216
+ }
1217
+
1218
+ /**
1219
+ * Whether an existing environment is the relocatable one install would leave: its config says
1220
+ * so, no launcher or interpreter link names an environment or a checkout (its own, a foreign
1221
+ * one, or a checkout's .devenv profile, in a shebang, a wrapper's exec argv, or the link
1222
+ * itself), and no `.pth` line is an absolute path. The stamp alone cannot witness this — a
1223
+ * copied checkout carries a matching
1224
+ * stamp and an old absolute launcher together, and admits the launcher forever.
1225
+ */
1226
+ /** One shell command line as its words: quotes group, backslash escapes, no expansion. */
1227
+ function shellWords(line: string): string[] {
1228
+ const words: string[] = [];
1229
+ let word = '';
1230
+ let quote: string | null = null;
1231
+ let started = false;
1232
+ for (let index = 0; index < line.length; index += 1) {
1233
+ const char = line[index] ?? '';
1234
+ if (quote !== null) {
1235
+ if (char === quote) quote = null;
1236
+ else if (char === '\\' && quote === '"' && index + 1 < line.length) word += line[++index];
1237
+ else word += char;
1238
+ continue;
1239
+ }
1240
+ if (char === "'" || char === '"') {
1241
+ quote = char;
1242
+ started = true;
1243
+ } else if (char === '\\' && index + 1 < line.length) {
1244
+ word += line[++index];
1245
+ started = true;
1246
+ } else if (/\s/.test(char)) {
1247
+ if (started || word !== '') words.push(word);
1248
+ word = '';
1249
+ started = false;
1250
+ } else {
1251
+ word += char;
1252
+ }
1253
+ }
1254
+ if (started || word !== '') words.push(word);
1255
+ return words;
1256
+ }
1257
+
1258
+ function uvEnvironmentRelocatable(environment: string): boolean {
1259
+ const config = readFileIfPresent(path.join(environment, 'pyvenv.cfg'));
1260
+ if (config === null || !/^relocatable\s*=\s*true\s*$/m.test(config.toString('utf8'))) return false;
1261
+ const environmentReal = realpathSync(environment);
1262
+ // Whether an absolute interpreter path names an environment or a checkout, as opposed to a
1263
+ // store or system interpreter, which names neither: it is inside this environment, inside
1264
+ // another environment (its home carries pyvenv.cfg), under a checkout's .devenv state, or
1265
+ // it no longer exists at all.
1266
+ const namesEnvironment = (interpreter: string): boolean => {
1267
+ if (interpreter.startsWith(`${environment}/`) || interpreter.startsWith(`${environmentReal}/`)) return true;
1268
+ if (!existsSync(interpreter)) return true;
1269
+ if (interpreter.split(path.sep).includes('.devenv')) return true;
1270
+ const home = path.dirname(path.dirname(interpreter));
1271
+ return existsSync(path.join(home, 'pyvenv.cfg')) && home !== environment && home !== environmentReal;
1272
+ };
1273
+ const bin = path.join(environment, 'bin');
1274
+ if (existsSync(bin)) {
1275
+ const buffer = Buffer.alloc(256);
1276
+ for (const name of readdirSync(bin)) {
1277
+ const file = path.join(bin, name);
1278
+ let size: number;
1279
+ let isFile: boolean;
1280
+ let isLink = false;
1281
+ try {
1282
+ const stat = statSync(file);
1283
+ size = stat.size;
1284
+ isFile = stat.isFile();
1285
+ isLink = lstatSync(file).isSymbolicLink();
1286
+ } catch {
1287
+ return false;
1288
+ }
1289
+ if (!isFile) continue;
1290
+ if (isLink) {
1291
+ // The interpreter link itself is witnessed lexically, but only when it leaves the
1292
+ // environment: a relative alias (bin/python3 -> python) is the layout uv itself
1293
+ // writes and stays inside it. An absolute link into another checkout's .devenv
1294
+ // state names that checkout even when its final target is a store interpreter,
1295
+ // and no shebang reading ever sees it.
1296
+ let linkTarget: string;
1297
+ try {
1298
+ linkTarget = readlinkSync(file);
1299
+ } catch {
1300
+ return false;
1301
+ }
1302
+ if (path.isAbsolute(linkTarget)) {
1303
+ const lexical = path.resolve(bin, linkTarget);
1304
+ if (lexical.split(path.sep).includes('.devenv')) return false;
1305
+ const linkHome = path.dirname(path.dirname(lexical));
1306
+ if (existsSync(path.join(linkHome, 'pyvenv.cfg')) && linkHome !== environment && linkHome !== environmentReal)
1307
+ return false;
1308
+ } else {
1309
+ // A relative alias that resolves outside the environment is judged by where it
1310
+ // lands, like an absolute one; only aliases staying inside are uv's own layout.
1311
+ const lexical = path.resolve(bin, linkTarget);
1312
+ if (
1313
+ !lexical.startsWith(`${environment}${path.sep}`) &&
1314
+ !lexical.startsWith(`${environmentReal}${path.sep}`) &&
1315
+ namesEnvironment(lexical)
1316
+ )
1317
+ return false;
1318
+ }
1319
+ }
1320
+ let head: string;
1321
+ try {
1322
+ const fd = openSync(file, 'r');
1323
+ try {
1324
+ head = buffer.toString('utf8', 0, readSync(fd, buffer, 0, buffer.length, 0));
1325
+ } finally {
1326
+ closeSync(fd);
1327
+ }
1328
+ } catch {
1329
+ return false;
1330
+ }
1331
+ const shebang = /^#!\s*(\/\S+)(?:\s+(.*))?$/.exec(head.split('\n')[0] ?? '');
1332
+ if (shebang === null) continue;
1333
+ const interpreter = shebang[1] ?? '';
1334
+ const program = path.basename(interpreter);
1335
+ if (program === 'env') {
1336
+ // An env trampoline naming a bare program resolves through PATH; one naming an
1337
+ // absolute interpreter (after any flags, -S included) is judged as that interpreter.
1338
+ const tokens = shellWords(shebang[2] ?? '');
1339
+ let index = 0;
1340
+ while (index < tokens.length && tokens[index]?.startsWith('-')) index += tokens[index] === '-u' ? 2 : 1;
1341
+ const target = tokens[index];
1342
+ if (target !== undefined && target.startsWith('/') && namesEnvironment(target)) return false;
1343
+ continue;
1344
+ }
1345
+ if (['sh', 'bash', 'dash', 'zsh'].includes(program)) {
1346
+ // A relocatable wrapper execs a self-locating interpreter; a legacy one execs an
1347
+ // absolute environment path. Only exec lines are judged, tokenized as shell words
1348
+ // (quotes group, so spaced paths stay whole; a $VAR expansion is not an absolute
1349
+ // path and is never classified).
1350
+ if (size > 262_144) return false;
1351
+ let body: string;
1352
+ try {
1353
+ body = readFileSync(file, 'utf8');
1354
+ } catch {
1355
+ return false;
1356
+ }
1357
+ for (const line of body.split('\n')) {
1358
+ // Producer headers are shell words too: a uv console script's polyglot first
1359
+ // line tokenizes to the same exec argv as a plain exec line.
1360
+ const words = shellWords(line);
1361
+ if (words[0] !== 'exec') continue;
1362
+ const target = words.find((word) => word.includes('/bin/python')) ?? words[1];
1363
+ if (target !== undefined && target.startsWith('/') && namesEnvironment(target)) return false;
1364
+ }
1365
+ continue;
1366
+ }
1367
+ if (namesEnvironment(interpreter)) return false;
1368
+ }
1369
+ }
1370
+ for (const sitePackages of sitePackagesDirectories(environment)) {
1371
+ for (const name of readdirSync(sitePackages).filter((entry) => entry.endsWith('.pth'))) {
1372
+ const content = readFileSync(path.join(sitePackages, name), 'utf8');
1373
+ if (content.split('\n').some((line) => path.isAbsolute(line))) return false;
1374
+ }
1375
+ }
1376
+ return true;
1377
+ }
1378
+
1206
1379
  function uvInstaller(inputs: readonly string[], options: { locked: boolean }): Installer | null {
1207
1380
  const python = flags.python;
1208
1381
  if (python === undefined || !existsSync(path.join(projectRoot, 'pyproject.toml'))) {
@@ -1226,15 +1399,20 @@ function uvInstaller(inputs: readonly string[], options: { locked: boolean }): I
1226
1399
  uv === null ? 'uv not on PATH' : realpathSync(uv),
1227
1400
  ];
1228
1401
  return {
1229
- isCurrent: () => readInstallStamp(stampPath)?.inputs === inputsDigest(identity, inputs),
1402
+ isCurrent: () => {
1403
+ const stampOk = readInstallStamp(stampPath)?.inputs === inputsDigest(identity, inputs);
1404
+ const witnessOk = uvEnvironmentRelocatable(environment);
1405
+ return stampOk && witnessOk;
1406
+ },
1230
1407
  install: async ({ quiet }) => {
1231
1408
  // An inherited VIRTUAL_ENV names some other environment; uv would warn
1232
1409
  // that it does not match the project environment and ignore it.
1233
1410
  const env = Object.fromEntries(Object.entries(process.env).filter(([name]) => name !== 'VIRTUAL_ENV'));
1234
- // pyvenv.cfg records `relocatable = true` for an environment uv created
1235
- // with --relocatable. uv venv refuses a directory that already holds one.
1236
- const config = readFileIfPresent(path.join(environment, 'pyvenv.cfg'));
1237
- if (config === null || !/^relocatable\s*=\s*true\s*$/m.test(config.toString('utf8'))) {
1411
+ // Replacement is gated on the same witness currentness uses, not only on the config
1412
+ // line: a valid config beside an absolute launcher or .pth would otherwise sync over
1413
+ // the escape and stamp it. uv venv refuses a directory that already holds an
1414
+ // environment, so a witness failure removes it first.
1415
+ if (!uvEnvironmentRelocatable(environment)) {
1238
1416
  rmSync(environment, { recursive: true, force: true });
1239
1417
  await runSetupCommand(`uv ${create.join(' ')}`, $`uv ${create}`.env(env), { quiet });
1240
1418
  }
@@ -1314,12 +1492,7 @@ function cargoFetcher(workspace: string): Installer {
1314
1492
  */
1315
1493
  function relativizeCheckoutPaths(environment: string): void {
1316
1494
  const root = `${realpathSync(projectRoot)}${path.sep}`;
1317
- const lib = path.join(environment, 'lib');
1318
- for (const python of existsSync(lib) ? readdirSync(lib) : []) {
1319
- const sitePackages = path.join(lib, python, 'site-packages');
1320
- if (!python.startsWith('python') || !existsSync(sitePackages)) {
1321
- continue;
1322
- }
1495
+ for (const sitePackages of sitePackagesDirectories(environment)) {
1323
1496
  const site = realpathSync(sitePackages);
1324
1497
  for (const name of readdirSync(site).filter((entry) => entry.endsWith('.pth'))) {
1325
1498
  const file = path.join(site, name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.4.27",
3
+ "version": "0.4.28",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",