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: windows path errors #18

Merged
merged 1 commit into from
Jul 7, 2022
Merged
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
Binary file added .github/assets/infracost-debug-log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ The Infracost VSCode extension requires you to have:
If you're having problems with the extension and your problem isn't any of the **known issues** above, you can find the Infracost extension logs using the following method:

1. Open the extension terminal using the top menu (Terminal->New Terminal)
2. Select **Output** and **log (Window)** from the dropdown
2. Select **Output** and **Infracost Debug** from the dropdown.
![](https://github.com/infracost/vscode-infracost/blob/master/.github/assets/infracost-debug-log.png?raw=true)
3. There are sometimes additional CLI logs hidden in the **log (Window)** output.
![](https://github.com/infracost/vscode-infracost/blob/master/.github/assets/error-logs.png?raw=true)

The log there might give you more information for a problem you can fix on your own, e.g. syntax errors. If it's something more ominous please [raise an issue](https://github.com/infracost/vscode-infracost/issues), so that we can identify and fix the problem. Please include as much of the log information as you can and any other helpful information like OS and VSCode workspace size.
Expand Down
45 changes: 37 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { gte } from 'semver';
import * as os from 'os';

const Handlebars = create();
const debugLog = vscode.window.createOutputChannel("Infracost Debug");

/**
* infracostStatusBar is a vscode status bar that sits on the bottom of the vscode editor.
Expand Down Expand Up @@ -294,6 +295,8 @@ class Workspace {
}

async init() {
debugLog.appendLine(`debug: initializing workspace`);

const folders = vscode.workspace.workspaceFolders;
if (!folders || folders?.length === 0) {
return;
Expand All @@ -309,17 +312,22 @@ class Workspace {

async fileChange(file: vscode.TextDocument) {
const isTfFile = /.*\.tf$/.test(file.fileName)
const filename = cleanFilename(file.uri.path);

if (!isTfFile) {
debugLog.appendLine(`debug: ignoring file change for path ${filename}, not a terraform`);
return;
}

setInfracostStatusLoading();
this.loading = true;
this.codeLensEventEmitter.fire();

const filename = file.uri.path;
debugLog.appendLine(`debug: detected file change for path ${filename}`);

const projects = this.filesToProjects[filename];
if (projects === undefined) {
debugLog.appendLine(`debug: no valid projects found for path ${filename}`);
setInfracostReadyStatus();
return {};
}
Expand Down Expand Up @@ -350,26 +358,28 @@ class Workspace {
}

async run(path: string, init: boolean = false): Promise<infracostJSON.RootObject | undefined> {
debugLog.appendLine(`debug: running Infracost in project: ${path}`);
try {
let cmd = `INFRACOST_CLI_PLATFORM=vscode infracost breakdown --path ${path} --format json --log-level info`

if (os.platform() == 'win32') {
cmd = `cmd /C "set INFRACOST_CLI_PLATFORM=vscode && infracost breakdown --path ${path} --format json --log-level info"`
}

debugLog.appendLine(`debug: running Infracost cmd ${cmd}`);

const { stdout, stderr } = await util.promisify(exec)(cmd);
const body = <infracostJSON.RootObject>JSON.parse(stdout);

for (const project of body.projects) {
debugLog.appendLine(`debug: found project ${project}`);

const projectPath = project.metadata.path;
const formatted = new Project(projectPath, body.currency, this.blockTemplate);
for (const resource of project.breakdown.resources) {
for (const call of resource.metadata.calls) {
let filename = call.filename;
if (filename.startsWith('c')) {
const slash = /\\+/gi;
filename = '/'+filename.replace(slash, '/');
}
const filename = cleanFilename(call.filename);
debugLog.appendLine(`debug: adding file: ${filename} to project: ${projectPath}`);

formatted.file(filename).block(call.blockName).resources.push(resource);

Expand Down Expand Up @@ -399,7 +409,7 @@ class Workspace {
}
}

console.error(`Infracost cmd error trace ${error}`);
debugLog.appendLine(`error: Infracost cmd error trace ${error}`);

if (init) {
vscode.window.showErrorMessage(`Could not run the infracost cmd in the ${path} directory. If this is a multi-project workspace please try opening just a single project. If this problem continues please open an issue here: https://github.com/infracost/vscode-infracost.`);
Expand All @@ -423,10 +433,14 @@ class InfracostLensProvider implements CodeLensProvider {

async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
const lenses: CodeLens[] = [];
const blocks = this.workspace.project(document.uri.path);
const filename = cleanFilename(document.uri.path);
debugLog.appendLine(`debug: providing codelens for file ${filename}`);

const blocks = this.workspace.project(filename);

const symbols = await commands.executeCommand<SymbolInformation[]>('vscode.executeDocumentSymbolProvider', document.uri);
if (symbols === undefined) {
debugLog.appendLine(`debug: no valid symbols found for file ${filename}`);
return lenses;
}

Expand All @@ -437,7 +451,10 @@ class InfracostLensProvider implements CodeLensProvider {

const line = document.lineAt(getRangeFromSymbol(sym).start);
const resourceKey = sym.name.replace(/\s+/g, '.').replace(/\"/g, '').replace(/^resource\./g, '');
debugLog.appendLine(`debug: evaluating symbol ${resourceKey}`);

if (blocks[resourceKey] !== undefined) {
debugLog.appendLine(`debug: found Infracost price for symbol ${resourceKey}`);
const block = blocks[resourceKey];
const cost = block.cost();

Expand Down Expand Up @@ -484,6 +501,18 @@ function is<T extends object>(o: object, propOrMatcher?: keyof T | ((o: any) =>
return value === undefined ? (o as any)[propOrMatcher] !== undefined : (o as any)[propOrMatcher] === value;
}

function cleanFilename(filename: string): string {
const replaceC = /^\/C/g
filename = filename.replace(replaceC, '/c')

if (filename.startsWith('c')) {
const slash = /\\+/gi;
filename = '/'+filename.replace(slash, '/');
}

return filename;
}

function setInfracostStatusLoading() {
infracostStatusBar.text = '$(sync~spin) Infracost';
infracostStatusBar.show();
Expand Down