Skip to content
Open
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
32 changes: 32 additions & 0 deletions apps/platform-api/src/common/file-path.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as path from 'path';

/**
* Normalize a user- or agent-supplied file path into a safe repo-relative path.
*
* Handles Windows drive prefixes and backslashes so server-side logic behaves
* consistently for snapshots created from Windows checkouts and cloud Linux runners.
*/
export function normalizeProjectFilePath(filepath: string): string {
return filepath
.replace(/^[A-Za-z]:/, '')
.replace(/\\/g, '/')
.replace(/^\/+/, '')
.split('/')
.filter((segment) => segment && segment !== '.' && segment !== '..')
.join('/');
}

/** Resolve a normalized project file path inside a workspace directory. */
export function resolveProjectFilePath(baseDir: string, filepath: string): string {
const normalizedPath = normalizeProjectFilePath(filepath);
return path.join(baseDir, normalizedPath);
Comment on lines +20 to +22
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Drop the preserved absolute path prefix before joining

When a snapshot contains an absolute file like C:\repo\package.json or /home/alice/repo/package.json, resolveProjectFilePath() writes it to <baseDir>/repo/package.json instead of <baseDir>/package.json. matchesProjectFilePath() still treats that same entry as the root manifest, so startPreview() and runBuild() invoke npm install from the workspace root (apps/platform-api/src/modules/preview/preview.service.ts:75-84, apps/platform-api/src/modules/sandbox/sandbox.service.ts:56-67), where no package.json exists. In the absolute-path cases this patch is meant to support, preview/build still fail.

Useful? React with 👍 / 👎.

}


/** Match a normalized path against a repo-relative target, tolerating accidental absolute prefixes. */
export function matchesProjectFilePath(filepath: string, expectedPath: string): boolean {
const normalizedPath = normalizeProjectFilePath(filepath);
const normalizedExpectedPath = normalizeProjectFilePath(expectedPath);

return normalizedPath === normalizedExpectedPath || normalizedPath.endsWith(`/${normalizedExpectedPath}`);
}
5 changes: 3 additions & 2 deletions apps/platform-api/src/modules/preview/preview.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as fs from 'fs/promises';
import * as path from 'path';
import * as net from 'net';
import { PreviewInstance, PreviewStatus } from './preview.interfaces';
import { matchesProjectFilePath, resolveProjectFilePath } from '../../common/file-path.utils';

/** Port range reserved for preview dev servers */
const PORT_RANGE_START = 5100;
Expand Down Expand Up @@ -76,7 +77,7 @@ export class PreviewService implements OnModuleDestroy {

// Check if there is a package.json to install deps
const hasPackageJson = files.some(
(f) => path.normalize(f.filepath) === 'package.json',
(f) => matchesProjectFilePath(f.filepath, 'package.json'),
);

if (hasPackageJson) {
Expand Down Expand Up @@ -197,7 +198,7 @@ export class PreviewService implements OnModuleDestroy {
await fs.mkdir(baseDir, { recursive: true });

for (const file of files) {
const fullPath = path.join(baseDir, file.filepath);
const fullPath = resolveProjectFilePath(baseDir, file.filepath);
const dir = path.dirname(fullPath);
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(fullPath, file.content, 'utf-8');
Expand Down
9 changes: 5 additions & 4 deletions apps/platform-api/src/modules/sandbox/sandbox.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from 'fs/promises';
import * as path from 'path';
import * as os from 'os';
import { BuildRequest, BuildResult } from './sandbox.interfaces';
import { matchesProjectFilePath, resolveProjectFilePath } from '../../common/file-path.utils';

const execAsync = promisify(exec);

Expand Down Expand Up @@ -54,7 +55,7 @@ export class SandboxService {

// Step 2: Check if package.json exists; if not, skip install
const hasPackageJson = request.files.some(
(f) => path.normalize(f.filepath) === 'package.json',
(f) => matchesProjectFilePath(f.filepath, 'package.json'),
);

if (hasPackageJson) {
Expand All @@ -76,8 +77,8 @@ export class SandboxService {
// Step 4: TypeScript type-check (only if tsconfig exists)
const hasTsConfig = request.files.some(
(f) =>
path.normalize(f.filepath) === 'tsconfig.json' ||
path.normalize(f.filepath) === 'tsconfig.app.json',
matchesProjectFilePath(f.filepath, 'tsconfig.json') ||
matchesProjectFilePath(f.filepath, 'tsconfig.app.json'),
);

if (hasTsConfig) {
Expand Down Expand Up @@ -145,7 +146,7 @@ export class SandboxService {
files: Array<{ filepath: string; content: string }>,
): Promise<void> {
for (const file of files) {
const fullPath = path.join(baseDir, file.filepath);
const fullPath = resolveProjectFilePath(baseDir, file.filepath);
const dir = path.dirname(fullPath);
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(fullPath, file.content, 'utf-8');
Expand Down
5 changes: 3 additions & 2 deletions apps/platform-api/src/modules/session/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SandboxService } from '../sandbox/sandbox.service';
import { PreviewService } from '../preview/preview.service';
import { DocsService } from '../docs/docs.service';
import { CreateSessionDto, SendMessageDto } from './session.dto';
import { matchesProjectFilePath, normalizeProjectFilePath } from '../../common/file-path.utils';

/** Shape of SSE events pushed to the client */
export interface SessionSseEvent {
Expand Down Expand Up @@ -749,7 +750,7 @@ export class SessionService {
private detectFramework(
files: Array<{ filepath: string; content: string }>,
): 'vite-react' | 'nextjs' | 'static' {
const filenames = files.map((f) => f.filepath.replace(/\\/g, '/'));
const filenames = files.map((f) => normalizeProjectFilePath(f.filepath));

if (filenames.some((f) => f.includes('vite.config'))) {
return 'vite-react';
Expand All @@ -759,7 +760,7 @@ export class SessionService {
}

// Check package.json for framework hints
const packageJson = files.find((f) => f.filepath === 'package.json');
const packageJson = files.find((f) => matchesProjectFilePath(f.filepath, 'package.json'));
if (packageJson) {
try {
const pkg = JSON.parse(packageJson.content);
Expand Down