Skip to content
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

fix: only display unknown settings warning for install command #3130

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-geese-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pnpm": patch
---

only display unknown settings for install command
6 changes: 6 additions & 0 deletions .changeset/strange-llamas-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/cli-utils": minor
"@pnpm/config": minor
---

add option to check unknown settings
2 changes: 2 additions & 0 deletions packages/cli-utils/src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default async function (
globalDirShouldAllowWrite?: boolean
rcOptionsTypes: Record<string, unknown>
workspaceDir: string | undefined
checkUnknownSetting?: boolean
}
) {
const { config, warnings } = await getConfig({
Expand All @@ -17,6 +18,7 @@ export default async function (
packageManager,
rcOptionsTypes: opts.rcOptionsTypes,
workspaceDir: opts.workspaceDir,
checkUnknownSetting: opts.checkUnknownSetting,
})
config.cliOptions = cliOptions

Expand Down
25 changes: 14 additions & 11 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export default async (
}
rcOptionsTypes?: Record<string, unknown>
workspaceDir?: string | undefined
checkUnknownSetting?: boolean
}
): Promise<{ config: Config, warnings: string[] }> => {
const packageManager = opts.packageManager ?? { name: 'pnpm', version: 'undefined' }
Expand Down Expand Up @@ -393,18 +394,20 @@ export default async (
}
pnpmConfig.enablePnp = pnpmConfig['nodeLinker'] === 'pnp'

const settingKeys = Object.keys({
...npmConfig?.sources?.workspace?.data,
...npmConfig?.sources?.project?.data,
}).filter(key => key.trim() !== '')
const unknownKeys = []
for (const key of settingKeys) {
if (!rcOptions.includes(key) && !key.startsWith('//') && !(key.startsWith('@') && key.endsWith(':registry'))) {
unknownKeys.push(key)
if (opts.checkUnknownSetting) {
const settingKeys = Object.keys({
...npmConfig?.sources?.workspace?.data,
...npmConfig?.sources?.project?.data,
}).filter(key => key.trim() !== '')
const unknownKeys = []
for (const key of settingKeys) {
if (!rcOptions.includes(key) && !key.startsWith('//') && !(key.startsWith('@') && key.endsWith(':registry'))) {
unknownKeys.push(key)
}
}
if (unknownKeys.length) {
warnings.push(`Your .npmrc file contains unknown setting: ${unknownKeys.join(', ')}`)
}
}
if (unknownKeys.length) {
warnings.push(`Your .npmrc file contains unknown setting: ${unknownKeys.join(', ')}`)
}

return { config: pnpmConfig, warnings }
Expand Down
11 changes: 11 additions & 0 deletions packages/config/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,20 @@ test('warn user unknown settings in npmrc', async () => {
name: 'pnpm',
version: '1.0.0',
},
checkUnknownSetting: true,
})

expect(warnings).toStrictEqual([
'Your .npmrc file contains unknown setting: typo-setting, mistake-setting',
])

const { warnings: noWarnings } = await getConfig({
cliOptions: {},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})

expect(noWarnings).toStrictEqual([])
})
2 changes: 2 additions & 0 deletions packages/pnpm/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ export default async function run (inputArgv: string[]) {
// When we just want to print the location of the global bin directory,
// we don't need the write permission to it. Related issue: #2700
const globalDirShouldAllowWrite = cmd !== 'root'
const checkUnknownSetting = cmd === 'install'
config = await getConfig(cliOptions, {
excludeReporter: false,
globalDirShouldAllowWrite,
rcOptionsTypes,
workspaceDir,
checkUnknownSetting,
}) as typeof config
config.forceSharedLockfile = typeof config.workspaceDir === 'string' && config.sharedWorkspaceLockfile === true
config.argv = argv
Expand Down