create-nx-workspace 24.0.4 → 24.0.6
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.
|
@@ -3,22 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createSandbox = createSandbox;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
|
-
const tmp_1 = require("tmp");
|
|
7
6
|
const ora_1 = tslib_1.__importDefault(require("ora"));
|
|
8
7
|
const path_1 = require("path");
|
|
9
8
|
const package_manager_1 = require("./utils/package-manager");
|
|
10
9
|
const child_process_utils_1 = require("./utils/child-process-utils");
|
|
11
10
|
const nx_version_1 = require("./utils/nx/nx-version");
|
|
12
11
|
const error_utils_1 = require("./utils/error-utils");
|
|
12
|
+
const temp_install_directory_1 = require("./utils/temp-install-directory");
|
|
13
13
|
/**
|
|
14
14
|
* Creates a temporary directory and installs Nx in it.
|
|
15
15
|
* @param packageManager package manager to use
|
|
16
16
|
* @returns directory where Nx is installed
|
|
17
17
|
*/
|
|
18
18
|
async function createSandbox(packageManager) {
|
|
19
|
+
const tmpDir = (0, temp_install_directory_1.createTempInstallDirectory)();
|
|
19
20
|
const installSpinner = (0, ora_1.default)(`Installing dependencies with ${packageManager}`).start();
|
|
20
21
|
const { install, preInstall } = (0, package_manager_1.getPackageManagerCommand)(packageManager);
|
|
21
|
-
const tmpDir = (0, tmp_1.dirSync)().name;
|
|
22
22
|
try {
|
|
23
23
|
(0, fs_1.writeFileSync)((0, path_1.join)(tmpDir, 'package.json'), JSON.stringify({
|
|
24
24
|
dependencies: {
|
|
@@ -47,6 +47,7 @@ async function createSandbox(packageManager) {
|
|
|
47
47
|
lines.push(`Exit code: ${exitCode}`);
|
|
48
48
|
}
|
|
49
49
|
if (logFile) {
|
|
50
|
+
(0, temp_install_directory_1.keepTempInstallDirectory)(tmpDir);
|
|
50
51
|
lines.push(`Log file: ${logFile}`);
|
|
51
52
|
}
|
|
52
53
|
lines.push(`\nPlease verify that "${install}" runs successfully in a temporary directory.`);
|
|
@@ -84,7 +84,7 @@ function getPackageManagerCommand(packageManager = detectPackageManager()) {
|
|
|
84
84
|
// bun doesn't current support programmatically reading config https://github.com/oven-sh/bun/issues/7140
|
|
85
85
|
return {
|
|
86
86
|
install: 'bun install --ignore-scripts',
|
|
87
|
-
exec: '
|
|
87
|
+
exec: 'bun',
|
|
88
88
|
globalAdd: 'bun install -g',
|
|
89
89
|
};
|
|
90
90
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an empty owner-only directory for a throwaway package install.
|
|
3
|
+
*
|
|
4
|
+
* Package managers and Node read config and modules from every ancestor of
|
|
5
|
+
* the install, so it lives under `~/.nx/tmp`, else under the OS temp dir, and
|
|
6
|
+
* only where that root and all of its ancestors are closed to other users.
|
|
7
|
+
* Throws when neither root qualifies or can be written. The OS does not reap
|
|
8
|
+
* it, so it is removed when the process exits unless `keepTempInstallDirectory`
|
|
9
|
+
* is called.
|
|
10
|
+
*
|
|
11
|
+
* Mirrors `createTempInstallDirectory` in packages/nx/src/utils/package-manager.ts;
|
|
12
|
+
* create-nx-workspace cannot depend on nx.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createTempInstallDirectory(): string;
|
|
15
|
+
/**
|
|
16
|
+
* Leaves `dir` in place at exit, for a failed install whose log the user is
|
|
17
|
+
* pointed at. The stale sweep removes it a day later.
|
|
18
|
+
*/
|
|
19
|
+
export declare function keepTempInstallDirectory(dir: string): void;
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTempInstallDirectory = createTempInstallDirectory;
|
|
4
|
+
exports.keepTempInstallDirectory = keepTempInstallDirectory;
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const os_1 = require("os");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const tmp_1 = require("tmp");
|
|
9
|
+
const error_utils_1 = require("./error-utils");
|
|
10
|
+
/**
|
|
11
|
+
* Creates an empty owner-only directory for a throwaway package install.
|
|
12
|
+
*
|
|
13
|
+
* Package managers and Node read config and modules from every ancestor of
|
|
14
|
+
* the install, so it lives under `~/.nx/tmp`, else under the OS temp dir, and
|
|
15
|
+
* only where that root and all of its ancestors are closed to other users.
|
|
16
|
+
* Throws when neither root qualifies or can be written. The OS does not reap
|
|
17
|
+
* it, so it is removed when the process exits unless `keepTempInstallDirectory`
|
|
18
|
+
* is called.
|
|
19
|
+
*
|
|
20
|
+
* Mirrors `createTempInstallDirectory` in packages/nx/src/utils/package-manager.ts;
|
|
21
|
+
* create-nx-workspace cannot depend on nx.
|
|
22
|
+
*/
|
|
23
|
+
function createTempInstallDirectory() {
|
|
24
|
+
const dir = createInPrivateInstallRoot();
|
|
25
|
+
if (tempInstallDirectories.size === 0) {
|
|
26
|
+
process.once('exit', removeTempInstallDirectories);
|
|
27
|
+
}
|
|
28
|
+
tempInstallDirectories.add(dir);
|
|
29
|
+
return dir;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Leaves `dir` in place at exit, for a failed install whose log the user is
|
|
33
|
+
* pointed at. The stale sweep removes it a day later.
|
|
34
|
+
*/
|
|
35
|
+
function keepTempInstallDirectory(dir) {
|
|
36
|
+
tempInstallDirectories.delete(dir);
|
|
37
|
+
}
|
|
38
|
+
const tempInstallDirectories = new Set();
|
|
39
|
+
function removeTempInstallDirectories() {
|
|
40
|
+
for (const dir of tempInstallDirectories) {
|
|
41
|
+
try {
|
|
42
|
+
(0, fs_1.rmSync)(dir, { recursive: true, force: true });
|
|
43
|
+
}
|
|
44
|
+
catch { }
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const STALE_INSTALL_DIRECTORY_MS = 24 * 60 * 60 * 1000;
|
|
48
|
+
/**
|
|
49
|
+
* Removes install dirs left behind by processes that never reached their exit
|
|
50
|
+
* handler. A day outlasts any realistic install, so a concurrent process's
|
|
51
|
+
* install survives.
|
|
52
|
+
*/
|
|
53
|
+
function removeStaleInstallDirectories(root) {
|
|
54
|
+
let entries;
|
|
55
|
+
try {
|
|
56
|
+
entries = (0, fs_1.readdirSync)(root);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const cutoff = Date.now() - STALE_INSTALL_DIRECTORY_MS;
|
|
62
|
+
for (const entry of entries) {
|
|
63
|
+
const path = (0, path_1.join)(root, entry);
|
|
64
|
+
try {
|
|
65
|
+
const stats = (0, fs_1.lstatSync)(path);
|
|
66
|
+
if (stats.isDirectory() &&
|
|
67
|
+
stats.mtimeMs < cutoff &&
|
|
68
|
+
(typeof process.getuid !== 'function' || stats.uid === process.getuid())) {
|
|
69
|
+
(0, fs_1.rmSync)(path, { recursive: true, force: true });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch { }
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const TMPDIR_INSTALL_ROOT = 'nx-installs';
|
|
76
|
+
/**
|
|
77
|
+
* An existing `~/.nx/tmp` can still refuse writes, for example under an agent
|
|
78
|
+
* sandbox, so a failed create there moves on to the OS temp dir.
|
|
79
|
+
*/
|
|
80
|
+
function createInPrivateInstallRoot() {
|
|
81
|
+
let homeRefusal = homeInstallRootRefusal();
|
|
82
|
+
if (typeof homeRefusal !== 'string') {
|
|
83
|
+
const { root } = homeRefusal;
|
|
84
|
+
try {
|
|
85
|
+
return createInstallDirectory(root);
|
|
86
|
+
}
|
|
87
|
+
catch (e) {
|
|
88
|
+
const code = e?.code;
|
|
89
|
+
homeRefusal = `${root} is not writable${code ? ` (${code})` : ''}`;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const tmp = (0, os_1.tmpdir)();
|
|
93
|
+
const fallbackRoot = (0, path_1.join)(tmp, TMPDIR_INSTALL_ROOT);
|
|
94
|
+
let tmpRefusal = privateTreeRefusal(tmp);
|
|
95
|
+
if (!tmpRefusal) {
|
|
96
|
+
if (isOwnDirClosedToOthers(fallbackRoot)) {
|
|
97
|
+
return createInstallDirectory(fallbackRoot);
|
|
98
|
+
}
|
|
99
|
+
tmpRefusal = `${fallbackRoot} is not a directory owned by you and closed to other users`;
|
|
100
|
+
}
|
|
101
|
+
if (process.env.NX_ALLOW_INSECURE_DIRECTORIES === 'true') {
|
|
102
|
+
console.warn(`NX_ALLOW_INSECURE_DIRECTORIES is set, so Nx is installing packages in ${tmp} although ${tmpRefusal}.`);
|
|
103
|
+
return (0, tmp_1.dirSync)({ tmpdir: tmp, prefix: 'nx-install-' }).name;
|
|
104
|
+
}
|
|
105
|
+
throw new error_utils_1.CnwError('SANDBOX_FAILED', 'Nx cannot install packages because it found no directory closed to other users to install into.\n' +
|
|
106
|
+
` ~/.nx/tmp: ${homeRefusal}.\n` +
|
|
107
|
+
` The OS temp directory: ${tmpRefusal}.\n` +
|
|
108
|
+
'Package managers and Node read config and modules from every parent of the install directory, so Nx will not install where another user could write to it or to any of its parents.\n' +
|
|
109
|
+
'Point TMPDIR at a directory only you can write to, or set NX_ALLOW_INSECURE_DIRECTORIES=true to install in the OS temp directory anyway.');
|
|
110
|
+
}
|
|
111
|
+
function createInstallDirectory(root) {
|
|
112
|
+
removeStaleInstallDirectories(root);
|
|
113
|
+
return (0, tmp_1.dirSync)({ tmpdir: root }).name;
|
|
114
|
+
}
|
|
115
|
+
function homeInstallRootRefusal() {
|
|
116
|
+
let home;
|
|
117
|
+
try {
|
|
118
|
+
home = (0, os_1.homedir)();
|
|
119
|
+
}
|
|
120
|
+
catch { }
|
|
121
|
+
if (!home || !(0, path_1.isAbsolute)(home) || isAtOrUnderSharedTmp(home)) {
|
|
122
|
+
return 'there is no home directory separate from the shared temp directory';
|
|
123
|
+
}
|
|
124
|
+
const nxDir = (0, path_1.join)(home, '.nx');
|
|
125
|
+
const root = (0, path_1.join)(nxDir, 'tmp');
|
|
126
|
+
for (const dir of [nxDir, root]) {
|
|
127
|
+
if (!isOwnDirClosedToOthers(dir)) {
|
|
128
|
+
return `${dir} is not a directory owned by you and closed to other users`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return privateTreeRefusal(root) ?? { root };
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Mirrors `privateTreeRefusal` in packages/nx/src/utils/package-manager.ts.
|
|
135
|
+
*/
|
|
136
|
+
function privateTreeRefusal(dir) {
|
|
137
|
+
if (typeof process.getuid !== 'function') {
|
|
138
|
+
try {
|
|
139
|
+
if ((0, fs_1.lstatSync)(dir).isDirectory()) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
catch { }
|
|
144
|
+
return `${dir} is not a real directory`;
|
|
145
|
+
}
|
|
146
|
+
for (const leaf of [(0, path_1.resolve)(dir), canonicalDir(dir)]) {
|
|
147
|
+
for (let p = leaf;; p = (0, path_1.dirname)(p)) {
|
|
148
|
+
const refusal = privatePathRefusal(p, p === leaf);
|
|
149
|
+
if (refusal) {
|
|
150
|
+
return refusal;
|
|
151
|
+
}
|
|
152
|
+
if ((0, path_1.dirname)(p) === p) {
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
function privatePathRefusal(path, isLeaf) {
|
|
160
|
+
let stats;
|
|
161
|
+
try {
|
|
162
|
+
stats = (0, fs_1.lstatSync)(path);
|
|
163
|
+
}
|
|
164
|
+
catch (e) {
|
|
165
|
+
const code = e?.code;
|
|
166
|
+
return `${path} could not be inspected${code ? ` (${code})` : ''}`;
|
|
167
|
+
}
|
|
168
|
+
const uid = process.getuid();
|
|
169
|
+
const trustedOwner = stats.uid === uid || (!isLeaf && stats.uid === 0);
|
|
170
|
+
if (stats.isSymbolicLink() && !isLeaf) {
|
|
171
|
+
return trustedOwner ? undefined : `${path} is owned by uid ${stats.uid}`;
|
|
172
|
+
}
|
|
173
|
+
if (!stats.isDirectory()) {
|
|
174
|
+
return stats.isSymbolicLink()
|
|
175
|
+
? `${path} is a symlink, not a real directory`
|
|
176
|
+
: `${path} is not a directory`;
|
|
177
|
+
}
|
|
178
|
+
if (stats.mode & 0o022) {
|
|
179
|
+
const mode = (stats.mode & 0o7777).toString(8).padStart(4, '0');
|
|
180
|
+
return `${path} is writable by other users (mode ${mode})`;
|
|
181
|
+
}
|
|
182
|
+
return trustedOwner ? undefined : `${path} is owned by uid ${stats.uid}`;
|
|
183
|
+
}
|
|
184
|
+
function isAtOrUnderSharedTmp(dir) {
|
|
185
|
+
const sharedTmp = (0, os_1.platform)() === 'win32' ? (0, os_1.tmpdir)() : '/tmp';
|
|
186
|
+
const fromSharedTmp = (0, path_1.relative)(canonicalDir(sharedTmp), canonicalDir(dir));
|
|
187
|
+
return (fromSharedTmp === '' ||
|
|
188
|
+
(fromSharedTmp !== '..' &&
|
|
189
|
+
!fromSharedTmp.startsWith('..' + path_1.sep) &&
|
|
190
|
+
!(0, path_1.isAbsolute)(fromSharedTmp)));
|
|
191
|
+
}
|
|
192
|
+
/** Real path of `dir`, resolving its nearest existing ancestor when it is missing. */
|
|
193
|
+
function canonicalDir(dir) {
|
|
194
|
+
const missing = [];
|
|
195
|
+
let candidate = (0, path_1.resolve)(dir);
|
|
196
|
+
while (true) {
|
|
197
|
+
try {
|
|
198
|
+
return (0, path_1.join)((0, fs_1.realpathSync)(candidate), ...missing);
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
if ((0, path_1.dirname)(candidate) === candidate) {
|
|
202
|
+
return (0, path_1.resolve)(dir);
|
|
203
|
+
}
|
|
204
|
+
missing.unshift((0, path_1.basename)(candidate));
|
|
205
|
+
candidate = (0, path_1.dirname)(candidate);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function isOwnDirClosedToOthers(dir) {
|
|
210
|
+
try {
|
|
211
|
+
(0, fs_1.mkdirSync)(dir, { mode: 0o700 });
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
if (e?.code !== 'EEXIST') {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
const stats = (0, fs_1.lstatSync)(dir);
|
|
220
|
+
if (!stats.isDirectory()) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
return (typeof process.getuid !== 'function' ||
|
|
224
|
+
(stats.uid === process.getuid() && !(stats.mode & 0o022)));
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|