Skip to content

Commit

Permalink
fix(container): compiling error with pnpm (#12934)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Jan 9, 2025
1 parent 5f16a4c commit 673a518
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-clocks-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a regression where the Astro Container didn't work during the build, using `pnpm`
2 changes: 1 addition & 1 deletion packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { extname } from 'node:path';
import MagicString from 'magic-string';
import type * as vite from 'vite';
import { normalizePath } from 'vite';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import {
appendForwardSlash,
Expand All @@ -10,6 +9,7 @@ import {
removeBase,
removeQueryString,
} from '../core/path.js';
import { normalizePath } from '../core/viteUtils.js';
import type { AstroSettings } from '../types/astro.js';
import { VALID_INPUT_FORMATS, VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
import type { ImageTransform } from './types.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { nodeLogDestination } from '../core/logger/node.js';
import { NOOP_MIDDLEWARE_FN } from '../core/middleware/noop-middleware.js';
import { removeLeadingForwardSlash } from '../core/path.js';
import { RenderContext } from '../core/render-context.js';
import { getParts, validateSegment } from '../core/routing/manifest/create.js';
import { getParts } from '../core/routing/manifest/parts.js';
import { getPattern } from '../core/routing/manifest/pattern.js';
import { validateSegment } from '../core/routing/manifest/segment.js';
import type { AstroComponentFactory } from '../runtime/server/index.js';
import type { ComponentInstance } from '../types/astro.js';
import type { AstroMiddlewareInstance, MiddlewareHandler, Props } from '../types/public/common.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/css-asset-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { GetModuleInfo, ModuleInfo } from 'rollup';
import crypto from 'node:crypto';
import npath from 'node:path';
import { fileURLToPath } from 'node:url';
import { normalizePath } from 'vite';
import type { AstroSettings } from '../../types/astro.js';
import { viteID } from '../util.js';
import { normalizePath } from '../viteUtils.js';
import { getTopLevelPageModuleInfos } from './graph.js';

// These pages could be used as base names for the chunk hashed name, but they are confusing
Expand Down
8 changes: 3 additions & 5 deletions packages/astro/src/core/compile/compile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import type { TransformResult } from '@astrojs/compiler';
import type { ResolvedConfig } from 'vite';

import { fileURLToPath } from 'node:url';
import type { TransformResult } from '@astrojs/compiler';
import { transform } from '@astrojs/compiler';
import { normalizePath } from 'vite';
import type { ResolvedConfig } from 'vite';
import type { AstroPreferences } from '../../preferences/index.js';
import type { AstroConfig } from '../../types/public/config.js';
import type { AstroError } from '../errors/errors.js';
import { AggregateError, CompilerError } from '../errors/errors.js';
import { AstroErrorData } from '../errors/index.js';
import { resolvePath } from '../viteUtils.js';
import { normalizePath, resolvePath } from '../viteUtils.js';
import { type PartialCompileCssResult, createStylePreprocessor } from './style.js';
import type { CompileCssResult } from './types.js';

Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/compile/style.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'node:fs';
import type { TransformOptions } from '@astrojs/compiler';
import { type ResolvedConfig, normalizePath, preprocessCSS } from 'vite';
import { type ResolvedConfig, preprocessCSS } from 'vite';
import { AstroErrorData, CSSError, positionAt } from '../errors/index.js';
import { normalizePath } from '../viteUtils.js';
import type { CompileCssResult } from './types.js';

export type PartialCompileCssResult = Pick<CompileCssResult, 'isGlobal' | 'dependencies'>;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/errors/dev/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { stripVTControlCharacters } from 'node:util';
import { escape } from 'html-escaper';
import { bold, underline } from 'kleur/colors';
import type { ESBuildTransformResult } from 'vite';
import { normalizePath } from 'vite';
import type { SSRError } from '../../../types/public/internal.js';
import { removeLeadingForwardSlashWindows } from '../../path.js';
import { normalizePath } from '../../viteUtils.js';
import { AggregateError, type ErrorWithMetadata } from '../errors.js';
import { AstroErrorData } from '../index.js';
import { codeFrame } from '../printer.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/middleware/vite-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Plugin as VitePlugin } from 'vite';
import { normalizePath } from 'vite';
import { getOutputDirectory } from '../../prerender/utils.js';
import type { AstroSettings } from '../../types/astro.js';
import { addRollupInput } from '../build/add-rollup-input.js';
Expand All @@ -8,6 +7,7 @@ import type { StaticBuildOptions } from '../build/types.js';
import { MIDDLEWARE_PATH_SEGMENT_NAME } from '../constants.js';
import { MissingMiddlewareForInternationalization } from '../errors/errors-data.js';
import { AstroError } from '../errors/index.js';
import { normalizePath } from '../viteUtils.js';

export const MIDDLEWARE_MODULE_ID = '\0astro-internal:middleware';
const NOOP_MIDDLEWARE = '\0noop-middleware';
Expand Down
28 changes: 2 additions & 26 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { routeComparator } from '../priority.js';
import { getRouteGenerator } from './generator.js';
import { getPattern } from './pattern.js';
import { getRoutePrerenderOption } from './prerender.js';
import { validateSegment } from './segment.js';

const require = createRequire(import.meta.url);

interface Item {
Expand All @@ -37,14 +39,6 @@ interface Item {
routeSuffix: string;
}

function countOccurrences(needle: string, haystack: string) {
let count = 0;
for (const hay of haystack) {
if (hay === needle) count += 1;
}
return count;
}

// Disable eslint as we're not sure how to improve this regex yet
// eslint-disable-next-line regexp/no-super-linear-backtracking
const ROUTE_DYNAMIC_SPLIT = /\[(.+?\(.+?\)|.+?)\]/;
Expand All @@ -71,24 +65,6 @@ export function getParts(part: string, file: string) {

return result;
}

export function validateSegment(segment: string, file = '') {
if (!file) file = segment;

if (segment.includes('][')) {
throw new Error(`Invalid route ${file} \u2014 parameters must be separated`);
}
if (countOccurrences('[', segment) !== countOccurrences(']', segment)) {
throw new Error(`Invalid route ${file} \u2014 brackets are unbalanced`);
}
if (
(/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) &&
file.endsWith('.astro')
) {
throw new Error(`Invalid route ${file} \u2014 rest parameter must be a standalone segment`);
}
}

/**
* Checks whether two route segments are semantically equivalent.
*
Expand Down
28 changes: 28 additions & 0 deletions packages/astro/src/core/routing/manifest/parts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { RoutePart } from '../../../types/public/index.js';

// Disable eslint as we're not sure how to improve this regex yet
// eslint-disable-next-line regexp/no-super-linear-backtracking
const ROUTE_DYNAMIC_SPLIT = /\[(.+?\(.+?\)|.+?)\]/;
const ROUTE_SPREAD = /^\.{3}.+$/;

export function getParts(part: string, file: string) {
const result: RoutePart[] = [];
part.split(ROUTE_DYNAMIC_SPLIT).map((str, i) => {
if (!str) return;
const dynamic = i % 2 === 1;

const [, content] = dynamic ? /([^(]+)$/.exec(str) || [null, null] : [null, str];

if (!content || (dynamic && !/^(?:\.\.\.)?[\w$]+$/.test(content))) {
throw new Error(`Invalid route ${file} — parameter name must match /^[a-zA-Z0-9_$]+$/`);
}

result.push({
content,
dynamic,
spread: dynamic && ROUTE_SPREAD.test(content),
});
});

return result;
}
24 changes: 24 additions & 0 deletions packages/astro/src/core/routing/manifest/segment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function validateSegment(segment: string, file = '') {
if (!file) file = segment;

if (segment.includes('][')) {
throw new Error(`Invalid route ${file} \u2014 parameters must be separated`);
}
if (countOccurrences('[', segment) !== countOccurrences(']', segment)) {
throw new Error(`Invalid route ${file} \u2014 brackets are unbalanced`);
}
if (
(/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) &&
file.endsWith('.astro')
) {
throw new Error(`Invalid route ${file} \u2014 rest parameter must be a standalone segment`);
}
}

function countOccurrences(needle: string, haystack: string) {
let count = 0;
for (const hay of haystack) {
if (hay === needle) count += 1;
}
return count;
}
2 changes: 1 addition & 1 deletion packages/astro/src/core/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { performance } from 'node:perf_hooks';
import { fileURLToPath } from 'node:url';
import { dim } from 'kleur/colors';
import { type HMRPayload, createServer } from 'vite';
import { normalizePath } from 'vite';
import { CONTENT_TYPES_FILE } from '../../content/consts.js';
import { getDataStoreFile, globalContentLayer } from '../../content/content-layer.js';
import { createContentTypesGenerator } from '../../content/index.js';
Expand Down Expand Up @@ -32,6 +31,7 @@ import {
import type { Logger } from '../logger/core.js';
import { createRouteManifest } from '../routing/index.js';
import { ensureProcessNodeEnv } from '../util.js';
import { normalizePath } from '../viteUtils.js';

export type SyncOptions = {
mode: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
isFrontmatterValid,
} from '@astrojs/markdown-remark';
import type { Plugin } from 'vite';
import { normalizePath } from 'vite';
import { safeParseFrontmatter } from '../content/utils.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import type { Logger } from '../core/logger/core.js';
import { isMarkdownFile, isPage } from '../core/util.js';
import { normalizePath } from '../core/viteUtils.js';
import { shorthash } from '../runtime/server/shorthash.js';
import type { AstroSettings } from '../types/astro.js';
import { createDefaultAstroMetadata } from '../vite-plugin-astro/metadata.js';
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/vite-plugin-scanner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { extname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { bold } from 'kleur/colors';
import type { Plugin as VitePlugin } from 'vite';
import { normalizePath } from 'vite';
import { warnMissingAdapter } from '../core/dev/adapter-validation.js';
import type { Logger } from '../core/logger/core.js';
import { getRoutePrerenderOption } from '../core/routing/manifest/prerender.js';
import { isEndpoint, isPage } from '../core/util.js';
import { rootRelativePath } from '../core/viteUtils.js';
import { normalizePath, rootRelativePath } from '../core/viteUtils.js';
import type { AstroSettings, ManifestData } from '../types/astro.js';

export interface AstroPluginScannerOptions {
Expand Down

0 comments on commit 673a518

Please sign in to comment.