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: Normalize relative_path and avoid emitting multiple Documents for same file #331

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 10 additions & 8 deletions src/ProjectIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,18 @@ export class ProjectIndexer {
const startTimestamp = Date.now()
const sourceFiles = this.program.getSourceFiles()

const filesToIndex: ts.SourceFile[] = []
const filesToIndexMap: Map<string, ts.SourceFile> = new Map()
// Visit every sourceFile in the program
for (const sourceFile of sourceFiles) {
const includes = this.config.fileNames.includes(sourceFile.fileName)
if (!includes) {
continue
}
filesToIndex.push(sourceFile)
const projectRelativePath = path.normalize(path.relative(this.options.cwd, sourceFile.fileName))
filesToIndexMap.set(projectRelativePath, sourceFile)
}

if (filesToIndex.length === 0) {
if (filesToIndexMap.size === 0) {
throw new Error(
`no indexable files in project '${this.options.projectDisplayName}'`
)
Expand All @@ -108,7 +109,7 @@ export class ProjectIndexer {
? new ProgressBar(
` ${this.options.projectDisplayName} [:bar] :current/:total :title`,
{
total: filesToIndex.length,
total: filesToIndexMap.size,
renderThrottle: 100,
incomplete: '_',
complete: '#',
Expand All @@ -119,9 +120,10 @@ export class ProjectIndexer {
)
: undefined
let lastWrite = startTimestamp
for (const [index, sourceFile] of filesToIndex.entries()) {
const title = path.relative(this.options.cwd, sourceFile.fileName)
jobs?.tick({ title })
let index = -1
for (const [projectRelativePath, sourceFile] of filesToIndexMap.entries()) {
index += 1
jobs?.tick({ title: projectRelativePath })
if (!this.options.progressBar) {
const now = Date.now()
const elapsed = now - lastWrite
Expand All @@ -131,7 +133,7 @@ export class ProjectIndexer {
}
}
const document = new scip.scip.Document({
relative_path: path.relative(this.options.cwd, sourceFile.fileName),
relative_path: projectRelativePath,
occurrences: [],
})
const input = new Input(sourceFile.fileName, sourceFile.getText())
Expand Down
7 changes: 7 additions & 0 deletions src/SnapshotTesting.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path'
import { Input } from './Input'
import { Range } from './Range'
import { scip } from './scip'
Expand Down Expand Up @@ -62,6 +63,12 @@ export function formatSnapshot(
const out: string[] = []
const symbolTable = getSymbolTable(doc)

if (path.normalize(doc.relative_path) !== doc.relative_path) {
throw new Error(
`Document path must be normalized: ${doc.relative_path}`
)
}

const externalSymbolTable: Map<string, scip.SymbolInformation> = new Map()
for (const externalSymbol of externalSymbols) {
externalSymbolTable.set(externalSymbol.symbol, externalSymbol)
Expand Down
Loading