Skip to content

Commit

Permalink
fix: config file not resolving (#219)
Browse files Browse the repository at this point in the history
Resolves an issue when the `--config-file` flag is used and the relative
path isn't correctly resolved. This affects both the lens and the tree
structure.

The previous fix for relative paths only addressed when `--path` was
used
  • Loading branch information
owenrumney authored Jul 8, 2024
1 parent a56300a commit e41ec3c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions 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.29",
"version": "0.2.30",
"publisher": "Infracost",
"license": "Apache-2.0",
"icon": "infracost-logo.png",
Expand Down Expand Up @@ -141,4 +141,4 @@
"handlebars": "^4.7.7",
"js-yaml": "^4.1.0"
}
}
}
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export type ConfigFile = {
export type ConfigProject = {
path: string;
name: string;
skip_autodetect: boolean;
};
7 changes: 5 additions & 2 deletions src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ export default class InfracostProjectProvider implements TreeDataProvider<Infrac
.sort((a: File, b: File): number => b.rawCost() - a.rawCost())
.reduce((arr: InfracostTreeItem[], f: File): InfracostTreeItem[] => {
const name = path.basename(f.name);
const filePath = path.relative(element.key, f.name);
const filePath =
process.platform === 'win32'
? path.resolve(element.key, name)
: path.resolve(element.key, f.name);

if (filePath === name) {
if (filePath === f.name) {
const item = new InfracostTreeItem(
`${element.key}|${f.name}`,
name,
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { commands, SymbolInformation, TextDocument } from 'vscode';
import { Buffer } from 'buffer';
import * as fs from 'fs';
import logger from './log';

export const CONFIG_FILE_NAME = 'infracost.yml';
Expand Down Expand Up @@ -37,3 +39,16 @@ export async function isValidTerraformFile(file: TextDocument): Promise<boolean>

return true;
}

export async function getFileEncoding(filepath: string): Promise<string> {
const d = Buffer.alloc(5, 0);
const fd = fs.openSync(filepath, 'r');
fs.readSync(fd, d, 0, 5, 0);
fs.closeSync(fd);

if (d[0] === 0xef && d[1] === 0xbb && d[2] === 0xbf) return 'utf8';
if (d[0] === 0xfe && d[1] === 0xff) return 'utf16be';
if (d[0] === 0xff && d[1] === 0xfe) return 'utf16le';

return 'utf8';
}
32 changes: 21 additions & 11 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
cleanFilename,
CONFIG_FILE_NAME,
CONFIG_TEMPLATE_NAME,
getFileEncoding,
isValidTerraformFile,
USAGE_FILE_NAME,
} from './utils';
Expand Down Expand Up @@ -115,7 +116,7 @@ export default class Workspace {

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

const key = filename.split(path.sep).join('/');
const key = filename.split(path.sep).join('/');
const projects =
this.filesToProjects[
Object.keys(this.filesToProjects).find(
Expand Down Expand Up @@ -211,7 +212,7 @@ export default class Workspace {
projects = await this.runBreakdown(changedProjectPaths);
}

await this.renderProjectTree(projects, changedProjectPaths.length > 0, hasConfigFile);
await this.renderProjectTree(projects, changedProjectPaths.length === 0, hasConfigFile);
return projects;
} catch (error) {
logger.error(`Infracost cmd error trace ${error}`);
Expand Down Expand Up @@ -249,14 +250,15 @@ export default class Workspace {
{}
);
logger.debug('filtering config file projects to only those that have changed');

const doc = <ConfigFile>load(readFileSync(configFilePath, 'utf8'));
const encoding = await getFileEncoding(configFilePath);
const doc = <ConfigFile>load(readFileSync(configFilePath, encoding as BufferEncoding));
doc.projects = doc.projects.filter((p) => changed[p.path]);

const str = dump(doc);
const tmpConfig = path.join(tmpdir(), CONFIG_FILE_NAME);
writeFileSync(tmpConfig, str);
args = ['--config-file', configFilePath];
logger.debug(`created temporary config file ${tmpConfig}`);
args = ['--config-file', tmpConfig];
logger.debug(`running "infracost breakdown --config-file" with changed projects`);
}

Expand Down Expand Up @@ -297,6 +299,13 @@ export default class Workspace {
return projects;
}

private determineResolvedFilename(projectPath: string, filename: string): string {
if (process.platform === 'win32') {
return path.resolve(projectPath, path.basename(filename));
}
return [this.root, path.resolve(path.relative(this.root, filename))].join('');
}

private async renderProjectTree(
projects: infracostJSON.Project[],
init: boolean,
Expand All @@ -314,12 +323,13 @@ export default class Workspace {
const name = hasConfigFile ? project.name : path.relative(this.root, projectPath);
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 = cleanFilename(path.resolve(call.filename));
logger.debug(`adding file: ${filename} to project: ${projectPath}`);

formatted.setBlock(filename, call.blockName, call.startLine).resources.push(resource);
this.addProjectToFile(filename, projectPath);
if (resource.metadata.calls) {
for (const call of resource.metadata.calls) {
const filename = this.determineResolvedFilename(projectPath, call.filename);
logger.debug(`adding file: ${filename} to project: ${projectPath}`);
formatted.setBlock(filename, call.blockName, call.startLine).resources.push(resource);
this.addProjectToFile(filename, projectPath);
}
}
}

Expand Down

0 comments on commit e41ec3c

Please sign in to comment.