From b109375902918c6983bf65e119573519dd57b858 Mon Sep 17 00:00:00 2001 From: lancelot Date: Fri, 9 Apr 2021 12:22:14 +0530 Subject: [PATCH 1/2] Update !url.startsWith condition in file.ts Signed-off-by: lancelot --- .../scaffolder-backend/src/scaffolder/stages/prepare/file.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.ts index 9eb380be13..728a67a588 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.ts @@ -21,7 +21,7 @@ import { PreparerBase, PreparerOptions } from './types'; export class FilePreparer implements PreparerBase { async prepare({ url, workspacePath }: PreparerOptions) { - if (!url.startsWith('file:///')) { + if (!url.startsWith('file://')) { throw new InputError(`Wrong location protocol, should be 'file', ${url}`); } From 7abec4dbcfbf2ff0757358cbe1923430539bdaab Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 8 Apr 2021 11:05:45 +0200 Subject: [PATCH 2/2] scaffolder-backend: fix file protocol check Signed-off-by: Patrik Oldsberg --- .changeset/forty-paws-drum.md | 5 ++++ .../scaffolder/stages/prepare/file.test.ts | 28 +++++++++++++++++-- .../src/scaffolder/stages/prepare/file.ts | 4 +-- 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 .changeset/forty-paws-drum.md diff --git a/.changeset/forty-paws-drum.md b/.changeset/forty-paws-drum.md new file mode 100644 index 0000000000..1f3b587b8a --- /dev/null +++ b/.changeset/forty-paws-drum.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Fix for the `file://` protocol check in the `FilePreparer` being too strict, breaking Windows. diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.test.ts index f6e5600a83..24a3ef4112 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.test.ts @@ -31,7 +31,7 @@ describe('File preparer', () => { const targetPath = path.resolve(workspacePath, 'template'); await preparer.prepare({ - url: `file:///${root}path/to/template`, + url: `file://${root}path/to/template`, logger, workspacePath, }); @@ -46,12 +46,34 @@ describe('File preparer', () => { await expect( preparer.prepare({ - url: 'file://not/full/path', + url: 'http://not/file/path', logger, workspacePath, }), ).rejects.toThrow( - "Wrong location protocol, should be 'file', file://not/full/path", + "Wrong location protocol, should be 'file', http://not/file/path", ); + + if (os.platform() === 'win32') { + // eslint-disable-next-line jest/no-conditional-expect + await expect( + preparer.prepare({ + url: 'file:///unix/file/path', + logger, + workspacePath, + }), + ).rejects.toThrow('File URL path must be absolute'); + } else { + // eslint-disable-next-line jest/no-conditional-expect + await expect( + preparer.prepare({ + url: 'file://not/full/path', + logger, + workspacePath, + }), + ).rejects.toThrow( + `File URL host must be "localhost" or empty on ${os.platform()}`, + ); + } }); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.ts index 728a67a588..b02c496ff5 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/file.ts @@ -25,11 +25,11 @@ export class FilePreparer implements PreparerBase { throw new InputError(`Wrong location protocol, should be 'file', ${url}`); } + const templatePath = fileURLToPath(url); + const targetDir = path.join(workspacePath, 'template'); await fs.ensureDir(targetDir); - const templatePath = fileURLToPath(url); - await fs.copy(templatePath, targetDir, { recursive: true, });