@@ -9,6 +9,8 @@ import { PlatformPackager } from "../platformPackager"
9
9
import { ResolvedFileSet , getDestinationPath } from "../util/appFileCopier"
10
10
import { detectUnpackedDirs } from "./unpackDetector"
11
11
import { Readable } from "stream"
12
+ import * as findUp from "find-up"
13
+ import { readdir } from "fs/promises"
12
14
13
15
/** @internal */
14
16
export class AsarPackager {
@@ -143,8 +145,9 @@ export class AsarPackager {
143
145
return { path : destination , streamGenerator, unpacked, type : "file" , stat : { mode : stat . mode , size } }
144
146
}
145
147
148
+ const rootDir = ( await findWorkspaceRoot ( fileSet . src ) ) ?? fileSet . src
146
149
const realPathFile = await fs . realpath ( file )
147
- const realPathRelative = path . relative ( fileSet . src , realPathFile )
150
+ const realPathRelative = path . relative ( rootDir , realPathFile )
148
151
const isOutsidePackage = realPathRelative . startsWith ( ".." )
149
152
if ( isOutsidePackage ) {
150
153
log . error ( { source : log . filePath ( file ) , realPathFile : log . filePath ( realPathFile ) } , `unable to copy, file is symlinked outside the package` )
@@ -231,3 +234,34 @@ export class AsarPackager {
231
234
}
232
235
}
233
236
}
237
+
238
+ async function findWorkspaceRoot ( startDir : string ) : Promise < string | undefined > {
239
+ // Look for the workspace marker files
240
+ const file = await findUp (
241
+ async directory => {
242
+ const dirContents = await readdir ( directory )
243
+ const marker = [ "pnpm-workspace.yaml" , "package.json" ] . find ( marker => dirContents . includes ( marker ) )
244
+ return dirContents . includes ( ".git" ) && ! marker ? findUp . stop : marker
245
+ } ,
246
+ { cwd : startDir }
247
+ )
248
+
249
+ if ( ! file ) {
250
+ return undefined
251
+ }
252
+
253
+ // If it's package.json, make sure it has "workspaces"
254
+ if ( file . endsWith ( "package.json" ) ) {
255
+ const pkg = await fs . readJson ( file )
256
+ const newDir = path . dirname ( file )
257
+ if ( pkg . workspaces || pkg . pnpm || pkg . workspaces ?. packages ) {
258
+ return newDir
259
+ } else {
260
+ // Keep looking up in case a parent has it
261
+ return findWorkspaceRoot ( path . dirname ( newDir ) )
262
+ }
263
+ }
264
+
265
+ // If it's pnpm-workspace.yaml, assume root
266
+ return path . dirname ( file )
267
+ }
0 commit comments