Skip to content

Commit

Permalink
fix resolution of filenames across windows/mac/linux (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
owenrumney authored Jun 19, 2024
1 parent 5e28fb9 commit fab8d01
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "infracost",
"displayName": "Infracost",
"description": "Cloud cost estimates for Terraform in your editor",
"version": "0.2.27",
"version": "0.2.28",
"publisher": "Infracost",
"license": "Apache-2.0",
"icon": "infracost-logo.png",
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class CLI {
INFRACOST_CLI_PLATFORM: 'vscode',
INFRACOST_NO_COLOR: 'true',
INFRACOST_SKIP_UPDATE_CHECK: 'true',
INFRACOST_GRAPH_EVALUATOR: 'true'
INFRACOST_GRAPH_EVALUATOR: 'true',
},
});

Expand Down
13 changes: 3 additions & 10 deletions src/lens.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
CodeLens,
CodeLensProvider,
Event,
TextDocument,
} from 'vscode';
import { CodeLens, CodeLensProvider, Event, TextDocument } from 'vscode';
import Workspace from './workspace';
import { cleanFilename } from './utils';
import logger from './log';
import { InfracostCommand } from './command';
import context from './context';
Expand All @@ -26,12 +20,11 @@ export default class InfracostLensProvider implements CodeLensProvider {
}

const lenses: CodeLens[] = [];
const filename = cleanFilename(document.uri.path);
const filename = document.uri.fsPath;
logger.debug(`providing codelens for file ${filename}`);

const blocks = this.workspace.project(filename);
for (const block of Object.values(blocks)) {
if (block.filename !== filename) {
if (block.filename.toLowerCase() !== filename.toLowerCase()) {
continue;
}

Expand Down
32 changes: 23 additions & 9 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ export default class Workspace {

logger.debug(`detected file change for path ${filename}`);

const projects = this.filesToProjects[filename];
const key = filename.split(path.sep).join('/');
const projects =
this.filesToProjects[
Object.keys(this.filesToProjects).find(
(k: string) => k.toLowerCase() === key.toLowerCase()
) || key
];

if (projects === undefined) {
logger.debug(
`no valid projects found for path ${filename} attempting to locate project for file`
Expand Down Expand Up @@ -155,10 +162,16 @@ export default class Workspace {

// TODO: determine or allow users to switch the project they are using.
project(filename: string): { [key: string]: Block } {
const projects = this.filesToProjects[filename];

if (projects && Object.keys(projects).length > 0) {
const project = Object.keys(projects)[0];
const key = filename.split(path.sep).join('/');
const projectKey =
this.filesToProjects[
Object.keys(this.filesToProjects).find(
(k: string) => k.toLowerCase() === key.toLowerCase()
) || key
];

if (projectKey && Object.keys(projectKey).length > 0) {
const project = Object.keys(projectKey)[0];
return this.projects[project].blocks;
}

Expand Down Expand Up @@ -302,7 +315,7 @@ export default class Workspace {
const formatted = new Project(name, projectPath, this.currency, this.blockTemplate);
for (const resource of project.breakdown.resources) {
for (const call of resource.metadata.calls) {
const filename = path.resolve(cleanFilename(call.filename));
const filename = cleanFilename(path.resolve(call.filename));
logger.debug(`adding file: ${filename} to project: ${projectPath}`);

formatted.setBlock(filename, call.blockName, call.startLine).resources.push(resource);
Expand All @@ -327,10 +340,11 @@ export default class Workspace {
}

private addProjectToFile(filename: string, projectPath: string) {
if (this.filesToProjects[filename] === undefined) {
this.filesToProjects[filename] = {};
const key = filename.split(path.sep).join('/');
if (this.filesToProjects[key] === undefined) {
this.filesToProjects[key] = {};
}

this.filesToProjects[filename][projectPath] = true;
this.filesToProjects[key][projectPath] = true;
}
}

0 comments on commit fab8d01

Please sign in to comment.