diff --git a/packages/@angular/cli/models/webpack-configs/common.ts b/packages/@angular/cli/models/webpack-configs/common.ts index ecb60283a19c..3b8751292ab3 100644 --- a/packages/@angular/cli/models/webpack-configs/common.ts +++ b/packages/@angular/cli/models/webpack-configs/common.ts @@ -99,11 +99,12 @@ export function getCommonConfig(wco: WebpackConfigOptions) { // specify a configuration flag. // Also prevent writing outside the project path. That is not overridable. const fullOutputPath = path.resolve(buildOptions.outputPath, asset.output); - if (!fullOutputPath.startsWith(projectRoot)) { - const message = 'An asset cannot be written to a location outside the project.'; - throw new SilentError(message); - } if (!fullOutputPath.startsWith(path.resolve(buildOptions.outputPath))) { + if (!fullOutputPath.startsWith(projectRoot)) { + const message = 'An asset cannot be written to a location outside the project.'; + throw new SilentError(message); + } + if (!asset.allowOutsideOutDir) { const message = 'An asset cannot be written to a location outside of the output path. ' + 'You can override this message by setting the `allowOutsideOutDir` ' diff --git a/tests/e2e/tests/build/assets.ts b/tests/e2e/tests/build/assets.ts index 9b7e91941339..4b5a9a07a9d7 100644 --- a/tests/e2e/tests/build/assets.ts +++ b/tests/e2e/tests/build/assets.ts @@ -1,3 +1,4 @@ +import * as path from 'path'; import { writeMultipleFiles, createDir, @@ -10,6 +11,10 @@ import { expectToFail } from '../../utils/utils'; import {getGlobalVariable} from '../../utils/env'; +const temp = require('temp'); +const tempDir = path.join(temp.mkdirSync('angular-cli-e2e-assets-'), 'out'); + + export default function () { // Disable parts of it in webpack tests. const ejected = getGlobalVariable('argv').eject; @@ -55,6 +60,21 @@ export default function () { })) .then(() => expectToFail(() => ng('build'))) + // This asset will not fail with the exception above. + .then(() => updateJsonFile('.angular-cli.json', configJson => { + const app = configJson['apps'][0]; + app['outDir'] = tempDir; + app['assets'] = [ + { 'glob': '**/*', 'input': '../node_modules/some-package/', 'output': tempDir, + 'allowOutsideOutDir': true } + ]; + })) + .then(() => ng('build')) + .then(() => updateJsonFile('.angular-cli.json', configJson => { + const app = configJson['apps'][0]; + app['outDir'] = 'dist'; + }) + // This asset should also fail from reading from outside the project. .then(() => updateJsonFile('.angular-cli.json', configJson => { const app = configJson['apps'][0];