ronantakizawa/codereview-bench huggingface dataset:
Repository: withastro/astro
File: packages/integrations/cloudflare/src/wrangler.ts (TypeScript)

Code before:
import { existsSync } from 'node:fs';
import type { PluginConfig } from '@cloudflare/vite-plugin';

/**
 * Checks whether a wrangler file exists at the given path
 * @param root
 */
export function hasWranglerConfig(root: URL) {
	return (
		existsSync(new URL('wrangler.jsonc', root)) ||
		existsSync(new URL('wrangler.toml', root)) ||
		existsSync(new URL('wrangler.json', root))
	);
}

/**
 * Returns the default wrangler configuration used by Astro Cloudflare configuration.
 */
export function defaultCloudflareConfig(): PluginConfig['config'] {
	return {
		name: 'test-application',
		compatibility_date: '2025-05-21',
		main: '@astrojs/cloudflare/entrypoints/server',
		assets: {
			directory: './dist',
			binding: 'ASSETS',
		},
		images: {
			binding: 'IMAGES',
		},
		kv_namespaces: [
			{
				binding: 'SESSION',
				id: 'SESSION',
			},
		],
	};
}


Review comment (suggestion):
```suggestion
		compatibility_date: new Date().toISOString().slice(0, 10),
```

Code after:
import { existsSync } from 'node:fs';
import type { PluginConfig } from '@cloudflare/vite-plugin';

/**
 * Checks whether a wrangler file exists at the given path
 * @param root
 */
export function hasWranglerConfig(root: URL) {
	return (
		existsSync(new URL('wrangler.jsonc', root)) ||
		existsSync(new URL('wrangler.toml', root)) ||
		existsSync(new URL('wrangler.json', root))
	);
}

/**
 * Returns the default wrangler configuration used by Astro Cloudflare configuration.
 */
export function defaultCloudflareConfig(): PluginConfig['config'] {
	return {
		// TODO: better way to handle name, maybe package.json#name ?
		name: 'test-application',
		compatibility_date: '2025-05-21',
		main: '@astrojs/cloudflare/entrypoints/server',
		assets: {
			directory: './dist',
			binding: 'ASSETS',
		},
		images: {
			binding: 'IMAGES',
		},
		kv_namespaces: [
			{
				binding: 'SESSION',
				id: 'SESSION',
			},
		],
	};
}
Repository: withastro/astro
File: packages/integrations/cloudflare/src/wrangler.ts (TypeScript)

Code before:
import { existsSync } from 'node:fs';
import type { PluginConfig } from '@cloudflare/vite-plugin';

/**
 * Checks whether a wrangler file exists at the given path
 * @param root
 */
export function hasWranglerConfig(root: URL) {
	return (
		existsSync(new URL('wrangler.jsonc', root)) ||
		existsSync(new URL('wrangler.toml', root)) ||
		existsSync(new URL('wrangler.json', root))
	);
}

/**
 * Returns the default wrangler configuration used by Astro Cloudflare configuration.
 */
export function defaultCloudflareConfig(): PluginConfig['config'] {
	return {
		name: 'test-application',
		compatibility_date: new Date().toISOString().slice(0, 10),
		main: '@astrojs/cloudflare/entrypoints/server',
		assets: {
			directory: './dist',
			binding: 'ASSETS',
		},
		images: {
			binding: 'IMAGES',
		},
		kv_namespaces: [
			{
				binding: 'SESSION',
				id: 'SESSION',
			},
		],
	};
}


Review comment (bug):
What's the reason for this? I assume `compatibility_date` means that we want compatibility as of a specific date. So any time we update the plugin we could bump this. But just always setting it to the current date would mean that if they make a breaking API change we are saying that we support that, even though we don't.

That's my interpretation of this field at least, I could be wrong.

Code after:
import { existsSync } from 'node:fs';
import type { PluginConfig } from '@cloudflare/vite-plugin';

/**
 * Checks whether a wrangler file exists at the given path
 * @param root
 */
export function hasWranglerConfig(root: URL) {
	return (
		existsSync(new URL('wrangler.jsonc', root)) ||
		existsSync(new URL('wrangler.toml', root)) ||
		existsSync(new URL('wrangler.json', root))
	);
}

/**
 * Returns the default wrangler configuration used by Astro Cloudflare configuration.
 */
export function defaultCloudflareConfig(): PluginConfig['config'] {
	return {
		// TODO: better way to handle name, maybe package.json#name ?
		name: 'test-application',
		compatibility_date: '2025-05-21',
		main: '@astrojs/cloudflare/entrypoints/server',
		assets: {
			directory: './dist',
			binding: 'ASSETS',
		},
		images: {
			binding: 'IMAGES',
		},
		kv_namespaces: [
			{
				binding: 'SESSION',
				id: 'SESSION',
			},
		],
	};
}
Repository: withastro/astro
File: packages/astro/src/cli/add/index.ts (TypeScript)

Code before:
export default {
	preprocess: vitePreprocess(),
}\n`,
	LIT_NPMRC: `\
# Lit libraries are required to be hoisted due to dependency issues.
public-hoist-pattern[]=*lit*
`,
	DB_CONFIG: `\
import { defineDb } from 'astro:db';

// https://astro.build/db/config
export default defineDb({
  tables: {}
});
`,
	DB_SEED: `\
import { db } from 'astro:db';

// https://astro.build/db/seed
export default async function seed() {
	// TODO
}
`,
	CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\
{
	"compatibility_date": "2025-05-21",
	"name": ${JSON.stringify(name)},
	"main": "@astrojs/cloudflare/entrypoints/server",
	"assets": {
		"directory": "./dist",
		"binding": "ASSETS"
	},
	"observability": {
		"enabled": true
	}
}`,
	CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`,
};

const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
	netlify: '@astrojs/netlify',
	vercel: '@astrojs/vercel',
	cloudflare: '@astrojs/cloudflare',
	node: '@astrojs/node',
};

export async function add(names: string[], { flags }: AddOptions) {
	ensureProcessNodeEnv('production');
	const inlineConfig = flagsToAstroInlineConfig(flags);
	const { userConfig } = await resolveConfig(inlineConfig, 'add');
	telemetry.record(eventCliSession('add', userConfig));

Review comment (suggestion):
I think here it makes sense to keep the dynamic generation:

> When you start your project, you should always set compatibility_date to the current date.

https://developers.cloudflare.com/workers/configuration/compatibility-dates/

Code after:
export default {
	preprocess: vitePreprocess(),
}\n`,
	LIT_NPMRC: `\
# Lit libraries are required to be hoisted due to dependency issues.
public-hoist-pattern[]=*lit*
`,
	DB_CONFIG: `\
import { defineDb } from 'astro:db';

// https://astro.build/db/config
export default defineDb({
  tables: {}
});
`,
	DB_SEED: `\
import { db } from 'astro:db';

// https://astro.build/db/seed
export default async function seed() {
	// TODO
}
`,
	CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\
{
	"compatibility_date": ${JSON.stringify(new Date().toISOString().slice(0, 10))},
	"compatibility_flags": ["global_fetch_strictly_public"],
	"name": ${JSON.stringify(name)},
	"main": "@astrojs/cloudflare/entrypoints/server",
	"assets": {
		"directory": "./dist",
		"binding": "ASSETS"
	},
	"observability": {
		"enabled": true
	}
}`,
	CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`,
};

const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
	netlify: '@astrojs/netlify',
	vercel: '@astrojs/vercel',
	cloudflare: '@astrojs/cloudflare',
	node: '@astrojs/node',
};

export async function add(names: string[], { flags }: AddOptions) {
	ensureProcessNodeEnv('production');
	const inlineConfig = flagsToAstroInlineConfig(flags);
	const { userConfig } = await resolveConfig(inlineConfig, 'add');
Repository: withastro/astro
File: packages/astro/src/cli/add/index.ts (TypeScript)

Code before:
export default {
	preprocess: vitePreprocess(),
}\n`,
	LIT_NPMRC: `\
# Lit libraries are required to be hoisted due to dependency issues.
public-hoist-pattern[]=*lit*
`,
	DB_CONFIG: `\
import { defineDb } from 'astro:db';

// https://astro.build/db/config
export default defineDb({
  tables: {}
});
`,
	DB_SEED: `\
import { db } from 'astro:db';

// https://astro.build/db/seed
export default async function seed() {
	// TODO
}
`,
	CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\
{
	"compatibility_date": "2025-05-21",
	"name": ${JSON.stringify(name)},
	"main": "@astrojs/cloudflare/entrypoints/server",
	"assets": {
		"directory": "./dist",
		"binding": "ASSETS"
	},
	"observability": {
		"enabled": true
	}
}`,
	CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`,
};

const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
	netlify: '@astrojs/netlify',
	vercel: '@astrojs/vercel',
	cloudflare: '@astrojs/cloudflare',
	node: '@astrojs/node',
};

export async function add(names: string[], { flags }: AddOptions) {
	ensureProcessNodeEnv('production');
	const inlineConfig = flagsToAstroInlineConfig(flags);
	const { userConfig } = await resolveConfig(inlineConfig, 'add');
	telemetry.record(eventCliSession('add', userConfig));

Review comment (suggestion):
We could export a util from `@cloudflare/vite-plugin` to get the latest `compatibility_date` supported by the installed version. You could then use that to provide the value here. Would that be helpful?

In the meantime, it would be good to use a more recent compatibility date.

Code after:
export default {
	preprocess: vitePreprocess(),
}\n`,
	LIT_NPMRC: `\
# Lit libraries are required to be hoisted due to dependency issues.
public-hoist-pattern[]=*lit*
`,
	DB_CONFIG: `\
import { defineDb } from 'astro:db';

// https://astro.build/db/config
export default defineDb({
  tables: {}
});
`,
	DB_SEED: `\
import { db } from 'astro:db';

// https://astro.build/db/seed
export default async function seed() {
	// TODO
}
`,
	CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\
{
	"compatibility_date": ${JSON.stringify(new Date().toISOString().slice(0, 10))},
	"compatibility_flags": ["global_fetch_strictly_public"],
	"name": ${JSON.stringify(name)},
	"main": "@astrojs/cloudflare/entrypoints/server",
	"assets": {
		"directory": "./dist",
		"binding": "ASSETS"
	},
	"observability": {
		"enabled": true
	}
}`,
	CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`,
};

const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
	netlify: '@astrojs/netlify',
	vercel: '@astrojs/vercel',
	cloudflare: '@astrojs/cloudflare',
	node: '@astrojs/node',
};

export async function add(names: string[], { flags }: AddOptions) {
	ensureProcessNodeEnv('production');
	const inlineConfig = flagsToAstroInlineConfig(flags);
	const { userConfig } = await resolveConfig(inlineConfig, 'add');
Repository: withastro/astro
File: packages/astro/src/cli/add/index.ts (TypeScript)

Code before:
public-hoist-pattern[]=*lit*
`,
	DB_CONFIG: `\
import { defineDb } from 'astro:db';

// https://astro.build/db/config
export default defineDb({
  tables: {}
});
`,
	DB_SEED: `\
import { db } from 'astro:db';

// https://astro.build/db/seed
export default async function seed() {
	// TODO
}
`,
	CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\
{
	"compatibility_date": "2025-05-21",
	"name": ${JSON.stringify(name)},
	"main": "@astrojs/cloudflare/entrypoints/server",
	"assets": {
		"directory": "./dist",
		"binding": "ASSETS"
	},
	"observability": {
		"enabled": true
	}
}`,
	CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`,
};

const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
	netlify: '@astrojs/netlify',
	vercel: '@astrojs/vercel',
	cloudflare: '@astrojs/cloudflare',
	node: '@astrojs/node',
};

export async function add(names: string[], { flags }: AddOptions) {
	ensureProcessNodeEnv('production');
	const inlineConfig = flagsToAstroInlineConfig(flags);
	const { userConfig } = await resolveConfig(inlineConfig, 'add');
	telemetry.record(eventCliSession('add', userConfig));
	if (flags.help || names.length === 0) {
		printHelp({
			commandName: 'astro add',
			usage: '[...integrations] [...adapters]',
			tables: {

Review comment (suggestion):
Are you setting `global_fetch_strictly_public` internally in the programmatic config? It's the right default for Astro because the Worker is the origin.

Code after:
public-hoist-pattern[]=*lit*
`,
	DB_CONFIG: `\
import { defineDb } from 'astro:db';

// https://astro.build/db/config
export default defineDb({
  tables: {}
});
`,
	DB_SEED: `\
import { db } from 'astro:db';

// https://astro.build/db/seed
export default async function seed() {
	// TODO
}
`,
	CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\
{
	"compatibility_date": ${JSON.stringify(new Date().toISOString().slice(0, 10))},
	"compatibility_flags": ["global_fetch_strictly_public"],
	"name": ${JSON.stringify(name)},
	"main": "@astrojs/cloudflare/entrypoints/server",
	"assets": {
		"directory": "./dist",
		"binding": "ASSETS"
	},
	"observability": {
		"enabled": true
	}
}`,
	CLOUDFLARE_ASSETSIGNORE: `_worker.js\n_routes.json`,
};

const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
	netlify: '@astrojs/netlify',
	vercel: '@astrojs/vercel',
	cloudflare: '@astrojs/cloudflare',
	node: '@astrojs/node',
};

export async function add(names: string[], { flags }: AddOptions) {
	ensureProcessNodeEnv('production');
	const inlineConfig = flagsToAstroInlineConfig(flags);
	const { userConfig } = await resolveConfig(inlineConfig, 'add');
	telemetry.record(eventCliSession('add', userConfig));
	if (flags.help || names.length === 0) {
		printHelp({
			commandName: 'astro add',
			usage: '[...integrations] [...adapters]',
Repository: withastro/astro
File: packages/language-tools/language-server/src/check.ts (TypeScript)

Code before:
				languagePlugins,
				services,
				tsconfigPath,
				includeProjectReference,
				({ project }) => {
					const { languageServiceHost } = project.typescript!;
					const astroInstall = getAstroInstall([this.workspacePath]);

					addAstroTypes(
						typeof astroInstall === 'string' ? undefined : astroInstall,
						this.ts,
						languageServiceHost,
					);
				},
			);
		} else {
			this.linter = kit.createTypeScriptInferredChecker(
				languagePlugins,
				services,
				() => {
					return globSync('**/*.astro', {
						cwd: this.workspacePath,
						ignore: ['node_modules'],
						absolute: true,
						expandDirectories: false,
					});
				},
				undefined,
				({ project }) => {
					const { languageServiceHost } = project.typescript!;
					const astroInstall = getAstroInstall([this.workspacePath]);

					addAstroTypes(
						typeof astroInstall === 'string' ? undefined : astroInstall,
						this.ts,
						languageServiceHost,
					);
				},
			);
		}
	}

	private getTsconfig() {
		if (this.tsconfigPath) {
			const tsconfig = resolve(this.workspacePath, this.tsconfigPath.replace(/^~/, homedir()));
			if (!existsSync(tsconfig)) {
				throw new Error(`Specified tsconfig file \`${tsconfig}\` does not exist.`);
			}
			return tsconfig;
		}


Review comment (suggestion):
```suggestion
						// Required to avoid tinyglobby running eternally
						expandDirectories: false,
```
Update the comment as you see fit, but just an explanation of the option would be great!

Code after:
				languagePlugins,
				services,
				tsconfigPath,
				includeProjectReference,
				({ project }) => {
					const { languageServiceHost } = project.typescript!;
					const astroInstall = getAstroInstall([this.workspacePath]);

					addAstroTypes(
						typeof astroInstall === 'string' ? undefined : astroInstall,
						this.ts,
						languageServiceHost,
					);
				},
			);
		} else {
			this.linter = kit.createTypeScriptInferredChecker(
				languagePlugins,
				services,
				() => {
					return globSync('**/*.astro', {
						cwd: this.workspacePath,
						ignore: ['node_modules'],
						absolute: true,
						// Required to avoid tinyglobby running eternally
						expandDirectories: false,
					});
				},
				undefined,
				({ project }) => {
					const { languageServiceHost } = project.typescript!;
					const astroInstall = getAstroInstall([this.workspacePath]);

					addAstroTypes(
						typeof astroInstall === 'string' ? undefined : astroInstall,
						this.ts,
						languageServiceHost,
					);
				},
			);
		}
	}

	private getTsconfig() {
		if (this.tsconfigPath) {
			const tsconfig = resolve(this.workspacePath, this.tsconfigPath.replace(/^~/, homedir()));
			if (!existsSync(tsconfig)) {
				throw new Error(`Specified tsconfig file \`${tsconfig}\` does not exist.`);
			}
			return tsconfig;
		}
Repository: withastro/astro
File: packages/astro/src/runtime/server/render/slot.ts (TypeScript)

Code before:

const slotString = Symbol.for('astro:slot-string');

export class SlotString extends HTMLString {
	public instructions: null | RenderInstruction[];
	public [slotString]: boolean;
	constructor(content: string, instructions: null | RenderInstruction[]) {
		super(content);
		this.instructions = instructions;
		this[slotString] = true;
	}
}

export function isSlotString(str: string): str is any {
	return !!(str as any)[slotString];
}

/**
 * Collects instructions from a SlotString into the target array.
 * Returns the (possibly newly created) instructions array.
 */
export function mergeSlotInstructions(
	target: RenderInstruction[] | null,
	source: SlotString,
): RenderInstruction[] | null {
	if (source.instructions) {
		target ??= [];
		target.push(...source.instructions);
	}
	return target;
}

export function renderSlot(
	result: SSRResult,
	slotted: ComponentSlotValue | RenderTemplateResult,
	fallback?: ComponentSlotValue | RenderTemplateResult,
): RenderInstance {
	if (!slotted && fallback) {
		return renderSlot(result, fallback);
	}
	return {
		async render(destination) {
			await renderChild(destination, typeof slotted === 'function' ? slotted(result) : slotted);
		},
	};
}

export async function renderSlotToString(
	result: SSRResult,
	slotted: ComponentSlotValue | RenderTemplateResult,
	fallback?: ComponentSlotValue | RenderTemplateResult,

Review comment (suggestion):
An empty array is truthy (`!![] => true`), which would create a new array without any real need.


```suggestion
	if (source.instructions?.length) {
```

Code after:

const slotString = Symbol.for('astro:slot-string');

export class SlotString extends HTMLString {
	public instructions: null | RenderInstruction[];
	public [slotString]: boolean;
	constructor(content: string, instructions: null | RenderInstruction[]) {
		super(content);
		this.instructions = instructions;
		this[slotString] = true;
	}
}

export function isSlotString(str: string): str is any {
	return !!(str as any)[slotString];
}

/**
 * Collects instructions from a SlotString into the target array.
 * Returns the (possibly newly created) instructions array.
 */
export function mergeSlotInstructions(
	target: RenderInstruction[] | null,
	source: SlotString,
): RenderInstruction[] | null {
	if (source.instructions?.length) {
		target ??= [];
		target.push(...source.instructions);
	}
	return target;
}

export function renderSlot(
	result: SSRResult,
	slotted: ComponentSlotValue | RenderTemplateResult,
	fallback?: ComponentSlotValue | RenderTemplateResult,
): RenderInstance {
	if (!slotted && fallback) {
		return renderSlot(result, fallback);
	}
	return {
		async render(destination) {
			await renderChild(destination, typeof slotted === 'function' ? slotted(result) : slotted);
		},
	};
}

export async function renderSlotToString(
	result: SSRResult,
	slotted: ComponentSlotValue | RenderTemplateResult,
	fallback?: ComponentSlotValue | RenderTemplateResult,
Repository: withastro/astro
File: packages/astro/src/core/session/types.ts (TypeScript)

Code before:
	 */
	driver: TDriver;
	/**
	 * Options for the unstorage driver
	 * @deprecated Use `import { sessionDrivers } from 'astro/config'` instead. This will be removed in Astro 7
	 */
	options?: NoInfer<BuiltinDriverOptions[TDriver]>;
}

interface CustomConfig extends BaseSessionConfig {
	/**
	 * Entrypoint for a custom session driver
	 * @deprecated Use the object shape (type `SessionDriverConfig`). This will be removed in Astro 7
	 */
	driver?: string;
	/**
	 * Options for the custom session driver
	 * @deprecated Use the object shape (type `SessionDriverConfig`). This will be removed in Astro 7
	 */
	options?: Record<string, unknown>;
}

export type SessionConfig<TDriver extends SessionDriverName | SessionDriverConfig | undefined> = [
	TDriver,
] extends [never]
	? CustomConfig
	: TDriver extends SessionDriverConfig
		? DriverConfig<TDriver>
		: TDriver extends keyof BuiltinDriverOptions
			? UnstorageConfig<TDriver>
			: CustomConfig;


Review comment (suggestion):
Is it possible to keep the unstorage config as the default somehow? IIRC from my tests it was the only way to keep autocomplete for now. Maybe try to update `UnstorageConfig` driver property to be optional

Code after:
	/**
	 * Entrypoint for an unstorage session driver
	 * @deprecated Use `import { sessionDrivers } from 'astro/config'` instead. This will be removed in Astro 7
	 */
	driver?: TDriver;
	/**
	 * Options for the unstorage driver
	 * @deprecated Use `import { sessionDrivers } from 'astro/config'` instead. This will be removed in Astro 7
	 */
	options?: TOptions;
}

interface CustomConfig extends BaseSessionConfig {
	/**
	 * Entrypoint for a custom session driver
	 * @deprecated Use the object shape (type `SessionDriverConfig`). This will be removed in Astro 7
	 */
	driver?: string;
	/**
	 * Options for the custom session driver
	 * @deprecated Use the object shape (type `SessionDriverConfig`). This will be removed in Astro 7
	 */
	options?: Record<string, unknown>;
}

export type SessionConfig<TDriver extends SessionDriverName | SessionDriverConfig | undefined> = [
	TDriver,
] extends [never]
	? UnstorageConfig<TDriver>
	: TDriver extends SessionDriverConfig
		? DriverConfig<TDriver>
		: TDriver extends keyof BuiltinDriverOptions
			? UnstorageConfig<TDriver>
			: CustomConfig;