Skip to content

Commit a921d56

Browse files
jdaltonmtorp
andcommitted
fix: handle directory targets according to specification
When a directory path is provided, it now recursively scans that directory for all files by appending /**/* to the path pattern. This ensures directory targets work as expected in scanning operations. Also fixes a type annotation issue in getWorkspaceGlobs. Cherry-picked from PR #794 (commit 5f78dfd) Original author: Martin Torp <martin@socket.dev> Co-Authored-By: Martin Torp <martin@socket.dev>
1 parent e70fc7f commit a921d56

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

src/utils/fs/glob.mts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import os from 'node:os'
22
import path from 'node:path'
3-
import type { SocketYml } from '@socketsecurity/config'
3+
4+
import fastGlob from 'fast-glob'
5+
import ignore from 'ignore'
6+
import micromatch from 'micromatch'
7+
import { parse as yamlParse } from 'yaml'
8+
49
import { NODE_MODULES } from '@socketsecurity/lib/constants/paths'
5-
import { safeReadFile } from '@socketsecurity/lib/fs'
10+
import { isDirSync, safeReadFile } from '@socketsecurity/lib/fs'
611
import { defaultIgnore } from '@socketsecurity/lib/globs'
712
import { readPackageJson } from '@socketsecurity/lib/packages'
813
import { transform } from '@socketsecurity/lib/streams'
914
import { isNonEmptyString } from '@socketsecurity/lib/strings'
10-
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
11-
import type { Options as GlobOptions } from 'fast-glob'
12-
import fastGlob from 'fast-glob'
13-
import ignore from 'ignore'
14-
import micromatch from 'micromatch'
15-
import { parse as yamlParse } from 'yaml'
15+
16+
1617
import { PNPM } from '../../constants/agents.mjs'
18+
1719
import type { Agent } from '../ecosystem/environment.mts'
20+
import type { SocketYml } from '@socketsecurity/config'
21+
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
22+
import type { Options as GlobOptions } from 'fast-glob'
1823

1924
const DEFAULT_IGNORE_FOR_GIT_IGNORE = defaultIgnore.filter(
2025
p => !p.endsWith('.gitignore'),
@@ -42,7 +47,7 @@ async function getWorkspaceGlobs(
4247
agent: Agent,
4348
cwd = process.cwd(),
4449
): Promise<string[]> {
45-
let workspacePatterns
50+
let workspacePatterns: string[] | undefined
4651
if (agent === PNPM) {
4752
const workspacePath = path.join(cwd, 'pnpm-workspace.yaml')
4853
const yml = await safeReadFile(workspacePath)
@@ -305,6 +310,13 @@ export function pathsToGlobPatterns(
305310
// Expand tilde paths.
306311
const expanded = expandTildePath(p)
307312
// Convert current directory references to glob patterns.
308-
return expanded === '.' || expanded === './' ? '**/*' : expanded
313+
if (expanded === '.' || expanded === './') {
314+
return '**/*'
315+
}
316+
// If the path is a directory, scan it recursively for all files.
317+
if (isDirSync(expanded)) {
318+
return `${expanded}/**/*`
319+
}
320+
return expanded
309321
})
310322
}

src/utils/fs/path-resolve.test.mts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import path from 'node:path'
22
import { fileURLToPath } from 'node:url'
3-
import { NODE_MODULES } from '@socketsecurity/lib/constants/paths'
4-
import { normalizePath } from '@socketsecurity/lib/path'
3+
54
import mockFs from 'mock-fs'
65
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import {
8-
PACKAGE_LOCK_JSON,
9-
PNPM_LOCK_YAML,
10-
YARN_LOCK,
11-
} from '../../constants/packages.mts'
6+
7+
import { NODE_MODULES } from '@socketsecurity/lib/constants/paths'
8+
import { normalizePath } from '@socketsecurity/lib/path'
9+
1210
import {
1311
findBinPathDetailsSync,
1412
findNpmDirPathSync,
1513
getPackageFilesForScan,
1614
} from './resolve.mts'
15+
import {
16+
PACKAGE_LOCK_JSON,
17+
PNPM_LOCK_YAML,
18+
YARN_LOCK,
19+
} from '../../constants/packages.mts'
1720

1821
const PACKAGE_JSON = 'package.json'
1922

@@ -178,6 +181,27 @@ describe('Path Resolve', () => {
178181
])
179182
})
180183

184+
it('should handle a directory path input', async () => {
185+
const subDirPath = normalizePath(path.join(mockFixturePath, 'subdir'))
186+
mockTestFs({
187+
[`${mockFixturePath}/package.json`]: '{}',
188+
[`${subDirPath}/package.json`]: '{}',
189+
[`${subDirPath}/nested/package.json`]: '{}',
190+
})
191+
192+
const actual = await sortedGetPackageFilesFullScans(
193+
[subDirPath],
194+
globPatterns,
195+
{
196+
cwd: mockFixturePath,
197+
},
198+
)
199+
expect(actual.map(normalizePath)).toEqual([
200+
`${subDirPath}/nested/package.json`,
201+
`${subDirPath}/package.json`,
202+
])
203+
})
204+
181205
it('should respect .gitignore', async () => {
182206
mockTestFs({
183207
[`${mockFixturePath}/.gitignore`]: 'bar/*\n!bar/package.json',

0 commit comments

Comments
 (0)