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

[heft] Fix an issue where heft would crash when copying static assets in --watch mode. #2557

Merged
merged 2 commits into from
Mar 17, 2021
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
56 changes: 33 additions & 23 deletions apps/heft/src/plugins/CopyFilesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ interface ICopyFileDescriptor {
hardlink: boolean;
}

export interface IResolvedDestinationCopyConfiguration extends IExtendedSharedCopyConfiguration {
/**
* Fully-qualified folder paths to which files should be copied.
*/
resolvedDestinationFolderPaths: string[];
}

export interface ICopyFilesOptions {
buildFolder: string;
copyConfigurations: IExtendedSharedCopyConfiguration[];
copyConfigurations: IResolvedDestinationCopyConfiguration[];
logger: ScopedLogger;
watchMode: boolean;
}
Expand Down Expand Up @@ -94,9 +101,16 @@ export class CopyFilesPlugin implements IHeftPlugin {
heftConfiguration
);

const copyConfigurations: IExtendedSharedCopyConfiguration[] = [];
const copyConfigurations: IResolvedDestinationCopyConfiguration[] = [];
for (const copyFilesEventAction of eventActions.copyFiles.get(heftEvent) || []) {
copyConfigurations.push(...copyFilesEventAction.copyOperations);
for (const copyOperation of copyFilesEventAction.copyOperations) {
copyConfigurations.push({
...copyOperation,
resolvedDestinationFolderPaths: copyOperation.destinationFolders.map((destinationFolder) =>
path.join(heftConfiguration.buildFolder, destinationFolder)
)
});
}
}

await this.runCopyAsync({
Expand Down Expand Up @@ -187,7 +201,7 @@ export class CopyFilesPlugin implements IHeftPlugin {

private async _getCopyFileDescriptorsAsync(
buildFolder: string,
copyConfigurations: IExtendedSharedCopyConfiguration[]
copyConfigurations: IResolvedDestinationCopyConfiguration[]
): Promise<ICopyFileDescriptor[]> {
// Create a map to deduplicate and prevent double-writes. The key in this map is the copy/link destination
// file path
Expand All @@ -208,13 +222,12 @@ export class CopyFilesPlugin implements IHeftPlugin {
);

// Dedupe and throw if a double-write is detected
for (const destinationFolderRelativePath of copyConfiguration.destinationFolders) {
for (const destinationFolderPath of copyConfiguration.resolvedDestinationFolderPaths) {
for (const sourceFileRelativePath of sourceFileRelativePaths) {
// Only include the relative path from the sourceFolder if flatten is false
const resolvedSourceFilePath: string = path.join(resolvedSourceFolderPath, sourceFileRelativePath);
const resolvedDestinationFilePath: string = path.resolve(
buildFolder,
destinationFolderRelativePath,
destinationFolderPath,
copyConfiguration.flatten ? '.' : path.dirname(sourceFileRelativePath),
path.basename(sourceFileRelativePath)
);
Expand Down Expand Up @@ -300,28 +313,25 @@ export class CopyFilesPlugin implements IHeftPlugin {
const globsToWatch: string[] = this._getIncludedGlobPatterns(copyConfiguration);
if (globsToWatch.length) {
const resolvedSourceFolderPath: string = path.join(buildFolder, copyConfiguration.sourceFolder);
const resolvedDestinationFolderPaths: string[] = copyConfiguration.destinationFolders.map(
(destinationFolder) => {
return path.join(buildFolder, destinationFolder);
}
);

const watcher: chokidar.FSWatcher = chokidar.watch(globsToWatch, {
cwd: resolvedSourceFolderPath,
ignoreInitial: true,
ignored: copyConfiguration.excludeGlobs
});

const copyAsset: (assetPath: string) => Promise<void> = async (assetPath: string) => {
const copyAsset: (relativeAssetPath: string) => Promise<void> = async (relativeAssetPath: string) => {
const { copiedFileCount, linkedFileCount } = await this.copyFilesAsync([
{
sourceFilePath: path.join(resolvedSourceFolderPath, assetPath),
destinationFilePaths: resolvedDestinationFolderPaths.map((resolvedDestinationFolderPath) => {
return path.join(
resolvedDestinationFolderPath,
copyConfiguration.flatten ? path.basename(assetPath) : assetPath
);
}),
sourceFilePath: path.join(resolvedSourceFolderPath, relativeAssetPath),
destinationFilePaths: copyConfiguration.resolvedDestinationFolderPaths.map(
(resolvedDestinationFolderPath) => {
return path.join(
resolvedDestinationFolderPath,
copyConfiguration.flatten ? path.basename(relativeAssetPath) : relativeAssetPath
);
}
),
hardlink: !!copyConfiguration.hardlink
}
]);
Expand All @@ -334,10 +344,10 @@ export class CopyFilesPlugin implements IHeftPlugin {

watcher.on('add', copyAsset);
watcher.on('change', copyAsset);
watcher.on('unlink', (assetPath) => {
watcher.on('unlink', (relativeAssetPath) => {
let deleteCount: number = 0;
for (const resolvedDestinationFolder of resolvedDestinationFolderPaths) {
FileSystem.deleteFile(path.resolve(resolvedDestinationFolder, assetPath));
for (const resolvedDestinationFolderPath of copyConfiguration.resolvedDestinationFolderPaths) {
FileSystem.deleteFile(path.resolve(resolvedDestinationFolderPath, relativeAssetPath));
deleteCount++;
}
logger.terminal.writeLine(`Deleted ${deleteCount} file${deleteCount === 1 ? '' : 's'}`);
Expand Down
28 changes: 17 additions & 11 deletions apps/heft/src/plugins/CopyStaticAssetsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { HeftSession } from '../pluginFramework/HeftSession';
import { HeftConfiguration } from '../configuration/HeftConfiguration';
import { IBuildStageContext, ICompileSubstage } from '../stages/BuildStage';
import { ScopedLogger } from '../pluginFramework/logging/ScopedLogger';
import { CoreConfigFiles, IExtendedSharedCopyConfiguration } from '../utilities/CoreConfigFiles';
import { CoreConfigFiles } from '../utilities/CoreConfigFiles';
import { ITypeScriptConfigurationJson } from './TypeScriptPlugin/TypeScriptPlugin';
import { CopyFilesPlugin } from './CopyFilesPlugin';
import { CopyFilesPlugin, IResolvedDestinationCopyConfiguration } from './CopyFilesPlugin';

const PLUGIN_NAME: string = 'CopyStaticAssetsPlugin';

Expand Down Expand Up @@ -79,7 +79,7 @@ export class CopyStaticAssetsPlugin extends CopyFilesPlugin {
compile.hooks.run.tapPromise(PLUGIN_NAME, async () => {
const logger: ScopedLogger = heftSession.requestScopedLogger('copy-static-assets');

const copyStaticAssetsConfiguration: IExtendedSharedCopyConfiguration = await this._loadCopyStaticAssetsConfigurationAsync(
const copyStaticAssetsConfiguration: IResolvedDestinationCopyConfiguration = await this._loadCopyStaticAssetsConfigurationAsync(
logger.terminal,
heftConfiguration
);
Expand All @@ -98,7 +98,7 @@ export class CopyStaticAssetsPlugin extends CopyFilesPlugin {
private async _loadCopyStaticAssetsConfigurationAsync(
terminal: Terminal,
heftConfiguration: HeftConfiguration
): Promise<IExtendedSharedCopyConfiguration> {
): Promise<IResolvedDestinationCopyConfiguration> {
const typescriptConfiguration:
| ITypeScriptConfigurationJson
| undefined = await CoreConfigFiles.typeScriptConfigurationFileLoader.tryLoadConfigurationFileForProjectAsync(
Expand All @@ -107,32 +107,38 @@ export class CopyStaticAssetsPlugin extends CopyFilesPlugin {
heftConfiguration.rigConfig
);

const destinationFolders: Set<string> = new Set<string>();
const resolvedDestinationFolderPaths: Set<string> = new Set<string>();
const destinationFolderNames: Set<string> = new Set<string>();

const tsconfigDestinationFolder: string | undefined = await this._tryGetTsconfigOutDirAsync(
const tsconfigDestinationFolderPath: string | undefined = await this._tryGetTsconfigOutDirPathAsync(
heftConfiguration.buildFolder,
terminal
);
if (tsconfigDestinationFolder) {
destinationFolders.add(tsconfigDestinationFolder);
if (tsconfigDestinationFolderPath) {
resolvedDestinationFolderPaths.add(tsconfigDestinationFolderPath);
destinationFolderNames.add(path.relative(heftConfiguration.buildFolder, tsconfigDestinationFolderPath));
}

for (const emitModule of typescriptConfiguration?.additionalModuleKindsToEmit || []) {
destinationFolders.add(emitModule.outFolderName);
resolvedDestinationFolderPaths.add(
path.resolve(heftConfiguration.buildFolder, emitModule.outFolderName)
);
destinationFolderNames.add(emitModule.outFolderName);
}

return {
...typescriptConfiguration?.staticAssetsToCopy,

// For now - these may need to be revised later
sourceFolder: 'src',
destinationFolders: Array.from(destinationFolders),
destinationFolders: Array.from(destinationFolderNames),
resolvedDestinationFolderPaths: Array.from(resolvedDestinationFolderPaths),
flatten: false,
hardlink: false
};
}

private async _tryGetTsconfigOutDirAsync(
private async _tryGetTsconfigOutDirPathAsync(
projectFolder: string,
terminal: Terminal
): Promise<string | undefined> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@rushstack/heft",
"comment": "Fix an issue where heft would crash when copying static assets in --watch mode.",
"type": "patch"
}
],
"packageName": "@rushstack/heft",
"email": "iclanton@users.noreply.github.com"
}