Repository navigation
fix(create): preserve native build approvals across pnpm versions #485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
49f63c7
fix: add pnpm build approvals for `sharp` and `workerd` in Cloudflare…
dario-piotrowicz 9a72e93
fix(create): preserve native build approvals across pnpm versions
tannerlinsley 3b4b188
fix(create): preserve standalone pnpm dependency additions
tannerlinsley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@tanstack/create': patch | ||
| --- | ||
|
|
||
| Write pnpm native build approvals to pnpm-workspace.yaml for pnpm 11 while retaining package.json approvals for pnpm 9 and 10. Preserve existing workspace configuration when adding integrations. | ||
|
|
||
| Keep ordinary pnpm 9 dependency additions working when creating a new standalone workspace, without changing existing monorepo root checks or explicit project settings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { dirnamePath, joinPaths } from './edge-path.js' | ||
| import type { Environment } from './types.js' | ||
|
|
||
| export function pnpmBuildDependencies( | ||
| packageJSON: Record<string, unknown>, | ||
| ): Array<string> { | ||
| const pnpm = packageJSON.pnpm | ||
| if (!pnpm || typeof pnpm !== 'object' || !('onlyBuiltDependencies' in pnpm)) | ||
| return [] | ||
| return Array.isArray(pnpm.onlyBuiltDependencies) | ||
| ? pnpm.onlyBuiltDependencies.filter( | ||
| (name): name is string => typeof name === 'string', | ||
| ) | ||
| : [] | ||
| } | ||
|
|
||
| export async function mergePnpmWorkspace( | ||
| existing: string | undefined, | ||
| dependencies: ReadonlyArray<string>, | ||
| existingDependencies: ReadonlyArray<string> = [], | ||
| ): Promise<string> { | ||
| const { Document, isMap, isScalar, isSeq, parseDocument } = await import( | ||
| 'yaml' | ||
| ) | ||
| const doc = | ||
| existing === undefined ? new Document({}) : parseDocument(existing) | ||
| if (doc.errors.length) | ||
| throw new Error(`Invalid pnpm-workspace.yaml: ${doc.errors[0].message}`) | ||
| if (doc.contents !== null && !isMap(doc.contents)) | ||
| throw new Error('pnpm-workspace.yaml must contain a mapping') | ||
| if (!doc.has('packages')) doc.set('packages', doc.createNode(['.'])) | ||
| const legacy = doc.get('onlyBuiltDependencies', true) | ||
| const names = new Set(dependencies) | ||
| if (isSeq(legacy)) { | ||
| for (const item of legacy.items) | ||
| if (isScalar(item) && typeof item.value === 'string') | ||
| names.add(item.value) | ||
| } | ||
| const approvals = doc.get('allowBuilds', true) | ||
| if (approvals !== undefined && !isMap(approvals)) | ||
| throw new Error('pnpm allowBuilds must contain a mapping') | ||
| // Existing denials and version/pattern rules are user policy, do not broaden them. | ||
| const customPolicy = | ||
| isMap(approvals) && | ||
| approvals.items.some( | ||
| ({ key, value }) => | ||
| !isScalar(key) || | ||
| typeof key.value !== 'string' || | ||
| /[*?{}[\]]|.+@/.test(key.value.replace(/^@/, '')) || | ||
| !isScalar(value) || | ||
| typeof value.value !== 'boolean', | ||
| ) | ||
| const missing = [...names].filter( | ||
| (name) => | ||
| (!isMap(approvals) || !approvals.has(name)) && | ||
| !(customPolicy && existingDependencies.includes(name)), | ||
| ) | ||
| if (customPolicy && missing.length) | ||
| throw new Error( | ||
| `Update the existing pnpm allowBuilds policy for: ${missing.join(', ')}`, | ||
| ) | ||
| if (!approvals) doc.set('allowBuilds', doc.createNode({})) | ||
| for (const name of missing) doc.setIn(['allowBuilds', name], true) | ||
| return doc.toString() | ||
| } | ||
|
|
||
| export async function writePnpmWorkspace( | ||
| environment: Environment, | ||
| path: string, | ||
| packageManager: string, | ||
| packageJSON: Record<string, unknown>, | ||
| context: { existingProject?: boolean; workspaceWasPresent?: boolean } = {}, | ||
| ) { | ||
| const dependencies = pnpmBuildDependencies(packageJSON) | ||
| if (packageManager !== 'pnpm' || !dependencies.length) return | ||
| const existing = environment.exists(path) | ||
| ? await environment.readFile(path) | ||
| : undefined | ||
| const workspace = await mergePnpmWorkspace(existing, dependencies) | ||
| await environment.writeFile(path, workspace) | ||
| if ( | ||
| !context.existingProject && | ||
| !(context.workspaceWasPresent ?? existing !== undefined) | ||
| ) { | ||
| const npmrcPath = joinPaths(dirnamePath(path), '.npmrc') | ||
| const npmrc = environment.exists(npmrcPath) | ||
| ? await environment.readFile(npmrcPath) | ||
| : undefined | ||
| const compatible = await standalonePnpmNpmrc(undefined, workspace, npmrc) | ||
| if (compatible !== undefined) | ||
| await environment.writeFile(npmrcPath, compatible) | ||
| } | ||
| } | ||
|
|
||
| export async function standalonePnpmNpmrc( | ||
| previousWorkspace: string | undefined, | ||
| workspace: string, | ||
| npmrc: string | undefined, | ||
| ): Promise<string | undefined> { | ||
| if (previousWorkspace !== undefined) return undefined | ||
| const { isScalar, isSeq, parseDocument } = await import('yaml') | ||
| const packages = parseDocument(workspace).get('packages', true) | ||
| if ( | ||
| !isSeq(packages) || | ||
| packages.items.length !== 1 || | ||
| !isScalar(packages.items[0]) || | ||
| packages.items[0].value !== '.' | ||
| ) | ||
| return undefined | ||
| const content = npmrc ?? '' | ||
| const bom = content.startsWith('\uFEFF') ? '\uFEFF' : '' | ||
| const body = bom ? content.slice(1) : content | ||
| // Only inspect the global INI section and preserve explicit user values. | ||
| const globalSection = body.split(/^\s*\[/m, 1)[0] | ||
| if (/^\s*["']?ignore-workspace-root-check["']?\s*=/im.test(globalSection)) | ||
| return content | ||
| // pnpm 9 does not read this setting from pnpm-workspace.yaml. | ||
| return `${bom}ignore-workspace-root-check=true\n${body}` | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.