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

Handle UTF-8 BOM #768

Merged
merged 4 commits into from
Jun 10, 2020
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
1 change: 1 addition & 0 deletions extension.bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export { mapJsonObjectValue } from "./src/util/mapJsonObjectValue";
export { indentMultilineString, unindentMultilineString as removeIndentation } from "./src/util/multilineStrings";
export * from "./src/util/nonNull";
export { normalizePath } from "./src/util/normalizePath";
export * from "./src/util/readUtf8FileWithBom";
export * from './src/util/time';
export { getVSCodePositionFromPosition } from "./src/util/vscodePosition";
export { wrapError } from "./src/util/wrapError";
Expand Down
5 changes: 3 additions & 2 deletions src/AzureRMTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { JsonOutlineProvider } from "./Treeview";
import { UnrecognizedBuiltinFunctionIssue } from "./UnrecognizedFunctionIssues";
import { getRenameError } from "./util/getRenameError";
import { normalizePath } from "./util/normalizePath";
import { readUtf8FileWithBom } from "./util/readUtf8FileWithBom";
import { Cancellation } from "./util/throwOnCancel";
import { onCompletionActivated, toVsCodeCompletionItem } from "./util/toVsCodeCompletionItem";
import { getVSCodeRangeFromSpan } from "./util/vscodePosition";
Expand Down Expand Up @@ -1199,7 +1200,7 @@ export class AzureRMTools {
}

// Nope, have to read it from disk
const contents = (await fse.readFile(uri.fsPath, { encoding: 'utf8' })).toString();
const contents = await readUtf8FileWithBom(uri.fsPath);
return new DeploymentTemplate(contents, uri);
}

Expand All @@ -1215,7 +1216,7 @@ export class AzureRMTools {
}

// Nope, have to read it from disk
const contents = (await fse.readFile(uri.fsPath, { encoding: 'utf8' })).toString();
const contents = await readUtf8FileWithBom(uri.fsPath);
return new DeploymentParameters(contents, uri);
}

Expand Down
5 changes: 3 additions & 2 deletions src/parameterFiles/parameterFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ext } from '../extensionVariables';
import { queryCreateParameterFile } from '../parameterFileGeneration';
import { containsParametersSchema } from '../schemas';
import { normalizePath } from '../util/normalizePath';
import { readUtf8FileWithBom } from '../util/readUtf8FileWithBom';
import { DeploymentFileMapping } from './DeploymentFileMapping';

const readAtMostBytesToFindParamsSchema = 4 * 1024;
Expand Down Expand Up @@ -47,8 +48,8 @@ export async function selectParameterFile(actionContext: IActionContext, mapping
let templateUri: Uri = sourceUri;

// Verify it's a template file (have to read in entire file to do full validation)
const contents = (await fse.readFile(templateUri.fsPath, { encoding: "utf8" })).toString();
const template: DeploymentTemplate = new DeploymentTemplate(contents, Uri.file("https://Check file is template"));
const contents = await readUtf8FileWithBom(templateUri.fsPath);
const template: DeploymentTemplate = new DeploymentTemplate(contents, templateUri);
if (!template.hasArmSchemaUri()) {
throw new Error(`"${templateUri.fsPath}" does not appear to be an Azure Resource Manager deployment template file.`);
}
Expand Down
15 changes: 15 additions & 0 deletions src/util/readUtf8FileWithBom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------

import * as fse from 'fs-extra';

const utf8Bom = 65279;

export async function readUtf8FileWithBom(fsPath: string): Promise<string> {
let contents = (await fse.readFile(fsPath, { encoding: "utf8" }));
if (contents.charCodeAt(0) === utf8Bom) {
contents = contents.slice(1);
StephenWeatherford marked this conversation as resolved.
Show resolved Hide resolved
}
return contents;
}
6 changes: 3 additions & 3 deletions test/support/TempFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import * as assert from 'assert';
import * as fs from 'fs';
import * as fse from 'fs-extra';
import * as path from 'path';
import { commands, TextDocument, TextEditor, Uri, window, workspace } from 'vscode';
import { readUtf8FileWithBom } from "../../extension.bundle";
import { getTempFilePath } from './getTempFilePath';

export class TempFile {
Expand All @@ -22,8 +22,8 @@ export class TempFile {
fs.writeFileSync(this.fsPath, contents);
}

public static fromExistingFile(filepath: string): TempFile {
const contents: string = fse.readFileSync(filepath).toString();
public static async fromExistingFile(filepath: string): Promise<TempFile> {
const contents: string = await readUtf8FileWithBom(filepath);
return new TempFile(contents, path.basename(filepath), path.extname(filepath));
}

Expand Down
2 changes: 1 addition & 1 deletion test/support/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export async function getDiagnosticsForTemplate(
paramsFile = new TempFile(unmarkedParams);
} else {
const absPath = path.join(testFolder, options.parametersFile!);
paramsFile = TempFile.fromExistingFile(absPath);
paramsFile = await TempFile.fromExistingFile(absPath);
}

// Map template to params
Expand Down