Skip to content

Commit d41edee

Browse files
author
Benjamin Pasero
committed
Workspace file: encoding should always be UTF-8 (fixes microsoft#46935)
1 parent f3b860d commit d41edee

3 files changed

Lines changed: 58 additions & 8 deletions

File tree

src/vs/workbench/services/files/electron-browser/fileService.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { isMacintosh, isWindows } from 'vs/base/common/platform';
2525
import product from 'vs/platform/node/product';
2626
import { Schemas } from 'vs/base/common/network';
2727
import { Severity, INotificationService, PromptOption } from 'vs/platform/notification/common/notification';
28+
import { WORKSPACE_EXTENSION } from 'vs/platform/workspaces/common/workspaces';
2829

2930
export class FileService implements IFileService {
3031

@@ -159,9 +160,16 @@ export class FileService implements IFileService {
159160

160161
private getEncodingOverrides(): IEncodingOverride[] {
161162
const encodingOverride: IEncodingOverride[] = [];
162-
encodingOverride.push({ resource: uri.file(this.environmentService.appSettingsHome), encoding: encoding.UTF8 });
163+
164+
// Global settings
165+
encodingOverride.push({ parent: uri.file(this.environmentService.appSettingsHome), encoding: encoding.UTF8 });
166+
167+
// Workspace files
168+
encodingOverride.push({ extension: WORKSPACE_EXTENSION, encoding: encoding.UTF8 });
169+
170+
// Folder Settings
163171
this.contextService.getWorkspace().folders.forEach(folder => {
164-
encodingOverride.push({ resource: uri.file(paths.join(folder.uri.fsPath, '.vscode')), encoding: encoding.UTF8 });
172+
encodingOverride.push({ parent: uri.file(paths.join(folder.uri.fsPath, '.vscode')), encoding: encoding.UTF8 });
165173
});
166174

167175
return encodingOverride;

src/vs/workbench/services/files/node/fileService.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import { Readable } from 'stream';
4242
import { Schemas } from 'vs/base/common/network';
4343

4444
export interface IEncodingOverride {
45-
resource: uri;
45+
parent?: uri;
46+
extension?: string;
4647
encoding: string;
4748
}
4849

@@ -974,9 +975,13 @@ export class FileService implements IFileService {
974975
for (let i = 0; i < this.options.encodingOverride.length; i++) {
975976
const override = this.options.encodingOverride[i];
976977

977-
// check if the resource is a child of the resource with override and use
978-
// the provided encoding in that case
979-
if (isParent(resource.fsPath, override.resource.fsPath, !isLinux /* ignorecase */)) {
978+
// check if the resource is child of encoding override path
979+
if (override.parent && isParent(resource.fsPath, override.parent.fsPath, !isLinux /* ignorecase */)) {
980+
return override.encoding;
981+
}
982+
983+
// check if the resource extension is equal to encoding override
984+
if (override.extension && paths.extname(resource.fsPath) === `.${override.extension}`) {
980985
return override.encoding;
981986
}
982987
}

src/vs/workbench/services/files/test/node/fileService.test.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ suite('FileService', () => {
912912
}, 100);
913913
});
914914

915-
test('options - encoding', function () {
915+
test('options - encoding override (parent)', function () {
916916

917917
// setup
918918
const _id = uuid.generateUuid();
@@ -922,7 +922,44 @@ suite('FileService', () => {
922922
return pfs.copy(_sourceDir, _testDir).then(() => {
923923
const encodingOverride: IEncodingOverride[] = [];
924924
encodingOverride.push({
925-
resource: uri.file(path.join(testDir, 'deep')),
925+
parent: uri.file(path.join(testDir, 'deep')),
926+
encoding: 'utf16le'
927+
});
928+
929+
const configurationService = new TestConfigurationService();
930+
configurationService.setUserConfiguration('files', { encoding: 'windows1252' });
931+
932+
const textResourceConfigurationService = new TestTextResourceConfigurationService(configurationService);
933+
934+
const _service = new FileService(new TestContextService(new Workspace(_testDir, _testDir, toWorkspaceFolders([{ path: _testDir }]))), TestEnvironmentService, textResourceConfigurationService, configurationService, new TestLifecycleService(), {
935+
encodingOverride,
936+
disableWatcher: true
937+
});
938+
939+
return _service.resolveContent(uri.file(path.join(testDir, 'index.html'))).then(c => {
940+
assert.equal(c.encoding, 'windows1252');
941+
942+
return _service.resolveContent(uri.file(path.join(testDir, 'deep', 'conway.js'))).then(c => {
943+
assert.equal(c.encoding, 'utf16le');
944+
945+
// teardown
946+
_service.dispose();
947+
});
948+
});
949+
});
950+
});
951+
952+
test('options - encoding override (extension)', function () {
953+
954+
// setup
955+
const _id = uuid.generateUuid();
956+
const _testDir = path.join(parentDir, _id);
957+
const _sourceDir = require.toUrl('./fixtures/service');
958+
959+
return pfs.copy(_sourceDir, _testDir).then(() => {
960+
const encodingOverride: IEncodingOverride[] = [];
961+
encodingOverride.push({
962+
extension: 'js',
926963
encoding: 'utf16le'
927964
});
928965

0 commit comments

Comments
 (0)