Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"toolPackages": [
{
"packageName": "@microsoft/api-extractor",
"packageVersion": "7.58.11"
"packageVersion": "7.60.1"
}
]
}
10 changes: 10 additions & 0 deletions common/changes/@microsoft/rush/deploy-tarballs_2026-11-25.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Add support for `.tar`, `.tar.gz`, and `.tgz` archives to `rush deploy --create-archive`.",
"type": "minor"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"changes": [
{
"packageName": "@rushstack/package-extractor",
"comment": "Support `.tar`, `.tar.gz`, and `.tgz` deployment archives using native tar, staging archive-only assets in a temporary folder.",
"type": "minor"
},
{
"packageName": "@rushstack/package-extractor",
"comment": "Expose shared, memoized native tar discovery and execution for use by Rush build caching.",
"type": "minor"
}
],
"packageName": "@rushstack/package-extractor"
}
3 changes: 3 additions & 0 deletions common/config/subspaces/default/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions common/reviews/api/package-extractor.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
import { IPackageJson } from '@rushstack/node-core-library';
import { ITerminal } from '@rushstack/terminal';

// @beta (undocumented)
export interface ICreateArchiveOptions extends ITarOptionsBase {
abortSignal?: AbortSignal;
// (undocumented)
archivePath: string;
baseFolderPath: string;
gzip?: boolean;
// (undocumented)
paths: string[];
}

// @public
export interface IExtractorDependencyConfiguration {
dependencyName: string;
Expand All @@ -25,6 +36,7 @@ export interface IExtractorMetadataJson {

// @public
export interface IExtractorOptions {
archiveTempFolder?: string;
Comment thread
iclanton marked this conversation as resolved.
Resolved
createArchiveFilePath?: string;
createArchiveOnly?: boolean;
dependencyConfigurations?: IExtractorDependencyConfiguration[];
Expand Down Expand Up @@ -76,6 +88,20 @@ export interface IProjectInfoJson {
projectName: string;
}

// @beta (undocumented)
export interface ITarOptionsBase {
// (undocumented)
logFilePath: string;
}

// @beta (undocumented)
export interface IUntarOptions extends ITarOptionsBase {
// (undocumented)
archivePath: string;
// (undocumented)
outputFolderPath: string;
}

// @public
export type LinkCreationMode = 'default' | 'script' | 'none';

Expand All @@ -86,6 +112,16 @@ export class PackageExtractor {
static getPackageIncludedFilesAsync(packageRootPath: string): Promise<string[]>;
}

// @beta
export class TarExecutable {
// (undocumented)
tryCreateArchiveAsync(options: ICreateArchiveOptions): Promise<number>;
// (undocumented)
static tryInitializeAsync(terminal: ITerminal, tarBinaryPath?: string): Promise<TarExecutable | undefined>;
// (undocumented)
tryUntarAsync(options: IUntarOptions): Promise<number>;
}

// (No @packageDocumentation comment for this package)

```
12 changes: 12 additions & 0 deletions libraries/package-extractor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

A library used for creating an isolated copy of a package and, optionally, bundling it into an archive.

The `createArchiveFilePath` option supports ZIP (`.zip`), tar (`.tar`), and gzip-compressed
tar (`.tar.gz` or `.tgz`) archives, including when `createArchiveOnly` is enabled.
Tar archives require the native `tar` executable on PATH. Archive-only tar deployments
stage assets in a unique timestamp-based folder under `archiveTempFolder` (default:
`common/temp` under the source root), then remove that folder after success or failure.
ZIP archives continue
to use JavaScript; archive-only ZIP deployments stage only generated files and read existing
source files directly. Materialized deployments archive their target files without staging.
Generated assets are written once by the asset handler; the archive manager accepts only file paths.

`TarExecutable` shares memoized native discovery and execution with Rush's build cache.

## Links

- [CHANGELOG.md](
Expand Down
1 change: 1 addition & 0 deletions libraries/package-extractor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"semver": "~7.7.4"
},
"devDependencies": {
"tar": "~7.5.6",
"local-node-rig": "workspace:*",
"@rushstack/heft-webpack5-plugin": "workspace:*",
"@rushstack/heft": "workspace:*",
Expand Down
137 changes: 102 additions & 35 deletions libraries/package-extractor/src/ArchiveManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import path from 'node:path';

import JSZip from 'jszip';

import { FileSystem, type FileSystemStats, Path } from '@rushstack/node-core-library';
import { Async, FileSystem, type FileSystemStats, Path } from '@rushstack/node-core-library';
import type { ITerminal } from '@rushstack/terminal';

import { TarExecutable } from './TarExecutable';
import { MAX_CONCURRENCY } from './scripts/createLinks/utilities/constants';

export type ArchiveFormat = 'zip' | 'tar' | 'tgz';

// 755 are default permissions to allow read/write/execute for owner and read/execute for group and others.
const DEFAULT_FILE_PERMISSIONS: number = 0o755;
Expand All @@ -14,51 +22,110 @@ const DEFAULT_FILE_PERMISSIONS: number = 0o755;
const SYMBOLIC_LINK_PERMISSIONS: number = 0o120000 | DEFAULT_FILE_PERMISSIONS;

export interface IAddToArchiveOptions {
filePath?: string;
fileData?: Buffer | string;
filePath: string;
archivePath: string;
stats?: FileSystemStats;
}

export interface ICreateArchiveOptions {
terminal: ITerminal;
sourceFolder: string;
}

export class ArchiveManager {
#zip: JSZip = new JSZip();
readonly #archiveFormat: ArchiveFormat;
readonly #entries: Map<string, IAddToArchiveOptions> = new Map();
#tarExecutablePromise: Promise<TarExecutable | undefined> | undefined;

public async addToArchiveAsync(options: IAddToArchiveOptions): Promise<void> {
const { filePath, fileData, archivePath } = options;

let data: Buffer | string;
let permissions: number;
if (filePath) {
const stats: FileSystemStats = options.stats ?? (await FileSystem.getLinkStatisticsAsync(filePath));
if (stats.isSymbolicLink()) {
data = await FileSystem.readLinkAsync(filePath);
permissions = SYMBOLIC_LINK_PERMISSIONS;
} else if (stats.isDirectory()) {
throw new Error('Directories cannot be added to the archive');
} else {
data = await FileSystem.readFileToBufferAsync(filePath);
permissions = stats.mode;
}
} else if (fileData) {
data = fileData;
permissions = DEFAULT_FILE_PERMISSIONS;
public constructor(archiveFormat: ArchiveFormat) {
this.#archiveFormat = archiveFormat;
}

public static getArchiveFormat(archiveFilePath: string): ArchiveFormat {
if (archiveFilePath.endsWith('.zip')) {
return 'zip';
} else if (archiveFilePath.endsWith('.tar')) {
return 'tar';
} else if (archiveFilePath.endsWith('.tar.gz') || archiveFilePath.endsWith('.tgz')) {
return 'tgz';
} else {
throw new Error('Either filePath or fileData must be provided');
throw new Error('Supported archive file extensions: .zip, .tar, .tar.gz, .tgz.');
}
}

// Replace backslashes for Unix compat
public async addToArchiveAsync(options: IAddToArchiveOptions): Promise<void> {
const { archivePath } = options;
const addPath: string = Path.convertToSlashes(archivePath);
this.#zip.file(addPath, data, {
unixPermissions: permissions,
dir: false
});
if (path.isAbsolute(addPath) || addPath.split('/').includes('..')) {
throw new Error('Archive entries must be relative paths within the archive');
}

this.#entries.set(addPath, options);
}

public async createArchiveAsync(archiveFilePath: string): Promise<void> {
const zipContent: Buffer = await this.#zip.generateAsync({
type: 'nodebuffer',
platform: 'UNIX'
});
await FileSystem.writeFileAsync(archiveFilePath, zipContent);
public async createArchiveAsync(archiveFilePath: string, options?: ICreateArchiveOptions): Promise<void> {
if (this.#archiveFormat === 'zip') {
const zip: JSZip = new JSZip();
const entries: { archivePath: string; data: Buffer | string; permissions: number }[] =
await Async.mapAsync(
this.#entries,
async ([archivePath, { filePath, stats: providedStats }]) => {
let data: Buffer | string;
let permissions: number;
const stats: FileSystemStats =
providedStats ?? (await FileSystem.getLinkStatisticsAsync(filePath));
if (stats.isSymbolicLink()) {
data = await FileSystem.readLinkAsync(filePath);
permissions = SYMBOLIC_LINK_PERMISSIONS;
} else if (stats.isDirectory()) {
throw new Error('Directories cannot be added to the archive');
} else {
data = await FileSystem.readFileToBufferAsync(filePath);
permissions = stats.mode;
}

return { archivePath, data, permissions };
},
{ concurrency: MAX_CONCURRENCY }
);
for (const { archivePath, data, permissions } of entries) {
zip.file(archivePath, data, {
unixPermissions: permissions,
dir: false
});
}

const zipContent: Buffer = await zip.generateAsync({
type: 'nodebuffer',
platform: 'UNIX'
});
await FileSystem.writeFileAsync(archiveFilePath, zipContent);
} else {
if (!options) {
throw new Error('Native tar creation requires terminal and source folder options');
}

const { terminal, sourceFolder } = options;
const tarExecutable: TarExecutable | undefined = await (this.#tarExecutablePromise ??=
TarExecutable.tryInitializeAsync(terminal));
if (!tarExecutable) {
throw new Error('Native tar is required to create tar archives. Ensure "tar" is on PATH.');
}

const logFilePath: string = `${archiveFilePath}.log`;
const exitCode: number = await tarExecutable.tryCreateArchiveAsync({
archivePath: archiveFilePath,
paths: [...this.#entries.keys()],
baseFolderPath: sourceFolder,
logFilePath,
gzip: this.#archiveFormat === 'tgz'
});
if (exitCode !== 0) {
await FileSystem.deleteFileAsync(archiveFilePath);
throw new Error(`Native tar exited with code ${exitCode}. See "${logFilePath}" for details.`);
} else {
await FileSystem.deleteFileAsync(logFilePath);
}
}
}
}
Loading