Skip to content

Commit 3cccc35

Browse files
authored
fix: Fixed --allow-duplicates flag in default (#249)
* fix: Fixed --allow-duplicates flag in default * ci(release): changed release flow to dispatch
1 parent f3a8209 commit 3cccc35

4 files changed

Lines changed: 43 additions & 11 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
name: Release
22

33
on:
4-
workflow_run:
5-
# Trigger this workflow when the "build, test and lint" workflow completes on the main branch
6-
workflows: ["build, test and lint"]
7-
types:
8-
- completed
9-
branches:
10-
- main
4+
workflow_dispatch:
115

126
permissions:
137
contents: write

src/cli/run.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ async function runScanMode(opts: Options): Promise<boolean> {
6666

6767
const { exitWithError } = await scanUsage({
6868
cwd: opts.cwd,
69+
allowDuplicates: opts.allowDuplicates,
6970
include: opts.includeFiles,
7071
exclude: opts.excludeFiles,
7172
ignore: opts.ignore,

src/commands/scanUsage.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { printComparisonError } from '../ui/scan/printComparisonError.js';
1414
import { hasIgnoreComment } from '../core/security/secretDetectors.js';
1515
import { frameworkValidator } from '../core/frameworks/frameworkValidator.js';
1616
import { detectSecretsInExample } from '../core/security/exampleSecretDetector.js';
17+
import { DEFAULT_EXAMPLE_FILE } from '../config/constants.js';
1718

1819
/**
1920
* Scans the codebase for environment variable usage and compares it with
@@ -86,7 +87,9 @@ export async function scanUsage(opts: ScanUsageOptions): Promise<ExitResult> {
8687
} else {
8788
scanResult = result.scanResult;
8889
comparedAgainst = result.comparedAgainst;
89-
duplicatesFound = result.duplicatesFound;
90+
if (result.duplicatesFound) {
91+
duplicatesFound = result.duplicatesFound;
92+
}
9093
fixApplied = result.fixApplied;
9194
removedDuplicates = result.removedDuplicates;
9295
fixedKeys = result.addedEnv;
@@ -101,7 +104,7 @@ export async function scanUsage(opts: ScanUsageOptions): Promise<ExitResult> {
101104
scanResult.inconsistentNamingWarnings =
102105
result.inconsistentNamingWarnings;
103106
}
104-
if (result.exampleFull && result.comparedAgainst === '.env.example') {
107+
if (result.exampleFull && result.comparedAgainst === DEFAULT_EXAMPLE_FILE) {
105108
scanResult.exampleWarnings = detectSecretsInExample(result.exampleFull);
106109
}
107110
}
@@ -128,7 +131,6 @@ export async function scanUsage(opts: ScanUsageOptions): Promise<ExitResult> {
128131
return {
129132
exitWithError:
130133
scanResult.missing.length > 0 ||
131-
duplicatesFound ||
132134
hasHighSeveritySecrets ||
133135
hasHighSeverityExampleWarnings ||
134136
!!(
@@ -155,7 +157,7 @@ export async function scanUsage(opts: ScanUsageOptions): Promise<ExitResult> {
155157
gitignoreUpdated,
156158
});
157159

158-
return { exitWithError: result.exitWithError || duplicatesFound };
160+
return { exitWithError: result.exitWithError };
159161
}
160162

161163
/**

test/e2e/cli.flags.e2e.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,41 @@ describe('duplicate detection', () => {
254254
expect(res.status).toBe(0);
255255
expect(res.stdout).not.toContain('Duplicate keys in .env');
256256
});
257+
258+
it('will detect duplicates in scan mode', () => {
259+
const cwd = tmpDir();
260+
fs.writeFileSync(path.join(cwd, '.env'), 'A=1\nA=2\nC=3\n');
261+
fs.writeFileSync(
262+
path.join(cwd, 'app.js'),
263+
`
264+
const api = process.env.A;
265+
const client = process.env.C;
266+
`,
267+
);
268+
269+
const res = runCli(cwd, ['--scan-usage']);
270+
expect(res.status).toBe(0);
271+
expect(res.stdout).toContain(
272+
'Duplicate keys in .env (last occurrence wins):',
273+
);
274+
expect(res.stdout).toContain('- A (2 occurrences)');
275+
});
276+
277+
it('--allow-duplicates works in scan mode', () => {
278+
const cwd = tmpDir();
279+
fs.writeFileSync(path.join(cwd, '.env'), 'A=1\nA=2\nC=3\n');
280+
fs.writeFileSync(
281+
path.join(cwd, 'app.js'),
282+
`
283+
const api = process.env.A;
284+
const client = process.env.C;
285+
`,
286+
);
287+
288+
const res = runCli(cwd, ['--scan-usage', '--allow-duplicates']);
289+
expect(res.status).toBe(0);
290+
expect(res.stdout).not.toContain('Duplicate keys in .env');
291+
});
257292
describe('--json output', () => {
258293
it('prints a JSON array with ok=true when files match', () => {
259294
const cwd = tmpDir();

0 commit comments

Comments
 (0)