Skip to content

Commit

Permalink
fix: resolution of overlapping root and tracked file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jdharvey-ibm committed Nov 17, 2023
1 parent 4be0b35 commit 8d7a282
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
36 changes: 34 additions & 2 deletions src/main/core/tracked-file-enumerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import path from 'path'

import { InvalidRootPathError } from '../exceptions/invalid-root-path-error.js'
import { Loggable } from './log/loggable.js'
import { Trace } from './log/trace.js'
import { runCommand } from './run-command.js'
Expand Down Expand Up @@ -42,7 +43,38 @@ export class TrackedFileEnumerator extends Loggable {

return allFiles
.filter((_file, index) => checks[index] === true)
.map((file) => path.relative(root, file))
.map((file) => path.resolve(root, file))
.map((file) => this.findAbsolutePath(file, root))
}

/**
* Given two paths that have partially overlapping directory hierarchies, find an absolute path
* that uses as many pieces of the two paths as possible.
*
* @param trackedFile - A file obtained from a git ls-tree command. This is NOT an absolute path.
* @param root - An absolute path representing a root director in which the tracked file is
* contained.
* @throws InvalidRootPathError if no valid path could be constructed using parts from both the
* trackedFile path and the root path.
* @returns A path.
*/
private findAbsolutePath(trackedFile: string, root: string) {
trackedFile = path.normalize(trackedFile)
root = path.normalize(root)
const trackedFileParts = trackedFile.split(path.sep)
const suffixParts: string[] = []

while (trackedFileParts.length > 0) {
if (root.endsWith(trackedFileParts.join(path.sep))) {
break
}

suffixParts.unshift(trackedFileParts.pop() ?? '')
}

if (trackedFileParts.length === 0) {
throw new InvalidRootPathError(trackedFile, root)
}

return path.join(root, ...suffixParts)
}
}
8 changes: 3 additions & 5 deletions src/main/scopes/jsx/jsx-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class JsxScope extends Scope {
const packageJsonTree = await getPackageJsonTree(this.root, this.logger)
const instrumentedPackage = await getPackageData(this.cwd, this.logger)
const sourceFiles = await getTrackedSourceFiles(this.root, this.logger)

const promises = sourceFiles.map(async (sourceFile) => {
const accumulator = new JsxElementAccumulator()

Expand Down Expand Up @@ -132,7 +133,6 @@ export class JsxScope extends Scope {
* @param accumulator - Accumulator to store results in.
* @param sourceFilePath - Absolute path to a sourceFile.
* @param packageJsonTree - Directory tree of package.json files.
* @returns Promise of all invokers getting resolved.
*/
async resolveInvokers(
accumulator: JsxElementAccumulator,
Expand All @@ -149,12 +149,10 @@ export class JsxScope extends Scope {
return
}

const promises = accumulator.elements.map(async (jsxElement) => {
const containingPackageData = await getPackageData(containingDir, this.logger)
const containingPackageData = await getPackageData(containingDir, this.logger)

accumulator.elements.forEach((jsxElement) => {
accumulator.elementInvokers.set(jsxElement, containingPackageData.name)
})

await Promise.allSettled(promises)
}
}
6 changes: 3 additions & 3 deletions src/main/scopes/npm/get-package-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export async function getPackageData(packagePath: string, logger: Logger): Promi
const result = await runCommand('npm pkg get name version', logger, {
cwd: packagePath
})
const parsed = JSON.parse(result.stdout)
const data = JSON.parse(result.stdout)

logger.traceExit('', 'getPackageData', parsed)
return parsed
logger.traceExit('', 'getPackageData', data)
return data
}
2 changes: 1 addition & 1 deletion src/test/scopes/jsx/tracked-file-enumerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('class: TrackedFileEnumerator', () => {
).resolves.toHaveLength(0)
})

it('returns correctly resolved relative paths', async () => {
it('returns correctly resolved absolute paths', async () => {
const root = new Fixture('projects/nested-project-files')

const files = await enumerator.find(root.path, (fileName) => fileName.endsWith('.js'))
Expand Down

0 comments on commit 8d7a282

Please sign in to comment.