Skip to content

[dotnet] [build] Migrate resource generation to dotnet tool#17388

Merged
nvborisenko merged 2 commits into
SeleniumHQ:trunkfrom
nvborisenko:dotnet-build-resource-generator
Apr 25, 2026
Merged

[dotnet] [build] Migrate resource generation to dotnet tool#17388
nvborisenko merged 2 commits into
SeleniumHQ:trunkfrom
nvborisenko:dotnet-build-resource-generator

Conversation

@nvborisenko

Copy link
Copy Markdown
Member

No python, purely dotnet/C#

💥 What does this PR do?

This pull request migrates the resource generation tool from Python to C#, ensuring that .NET developers can easily build and maintain it. The Python implementation is removed and replaced with a new C# console tool, and the Bazel build configuration is updated accordingly.

Migration from Python to C# for resource generation:

  • Added a new C# console tool GenerateResourcesTool.cs that generates the ResourceUtilities partial class with embedded JS resources, replacing the previous Python script. The tool processes --input and --output arguments and emits C# raw string literals with five quotes.
  • Removed the old Python script generate_resources_tool.py, eliminating all Python-specific logic for resource generation.
  • Updated the Bazel build file BUILD.bazel to use csharp_binary for building the new C# tool instead of py_binary, and to reference the new C# source file.
  • Updated comments and documentation in generate_resources.bzl to reflect the migration from a Python to a C# implementation.

🤖 AI assistance

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s):
    • What was generated:
    • I reviewed all AI output and can explain the change

🔄 Types of changes

  • Cleanup (formatting, renaming)

Copilot AI review requested due to automatic review settings April 25, 2026 08:24
@selenium-ci selenium-ci added C-dotnet .NET Bindings B-build Includes scripting, bazel and CI integrations labels Apr 25, 2026
@qodo-code-review

Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Migrate resource generation tool from Python to C#

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Migrate resource generation tool from Python to C#
• Replace py_binary with csharp_binary in Bazel build
• Implement GenerateResourcesTool.cs with argument parsing
• Remove legacy Python script and update documentation

Grey Divider

File Changes

1. dotnet/private/GenerateResourcesTool.cs ✨ Enhancement +77/-0

New C# resource generation tool implementation

• New C# console tool that generates ResourceUtilities partial class
• Parses --output and --input arguments with IDENT=path format
• Reads JS files and embeds them as C# raw string literals with 5-quotes
• Creates output directory if needed and writes UTF-8 encoded file

dotnet/private/GenerateResourcesTool.cs


2. dotnet/private/generate_resources.bzl 📝 Documentation +2/-2

Update documentation for C# migration

• Update docstring to reflect C# tool instead of Python script
• Change description from "via a Python tool" to generic implementation

dotnet/private/generate_resources.bzl


3. dotnet/private/generate_resources_tool.py Cleanup +0/-87

Remove legacy Python resource generation tool

• Remove entire Python implementation (87 lines)
• Eliminates Python-specific argument parsing and file handling logic

dotnet/private/generate_resources_tool.py


View more (1)
4. dotnet/private/BUILD.bazel ⚙️ Configuration changes +4/-4

Update Bazel build to use C# binary

• Replace py_binary rule with csharp_binary for generate_resources_tool
• Update srcs from generate_resources_tool.py to GenerateResourcesTool.cs
• Add target_frameworks = ["net10.0"] configuration
• Update load statement to import csharp_binary from rules_dotnet

dotnet/private/BUILD.bazel


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (1) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Unvalidated --input identifier/content 📘 Rule violation ≡ Correctness
Description
--input values are only superficially parsed and then directly used to emit C# ({name} and a
fixed 5-quote raw string delimiter), so invalid identifiers or JS content containing """"" can
cause later compiler failures instead of a clear upfront error.
Code

dotnet/private/GenerateResourcesTool.cs[R57-65]

+foreach (var (name, path) in inputs)
+{
+    var content = File.ReadAllText(path, Encoding.UTF8);
+    // Use a C# raw string literal with five quotes.
+    // The content must start on a new line and the closing quotes
+    // must be on their own line as well.
+    sb.AppendLine($"    internal const string {name} = {new string('"', 5)}");
+    sb.AppendLine(content);
+    sb.AppendLine($"{new string('"', 5)};");
Evidence
PR Compliance ID 12 requires validating configuration/external inputs early with clear, actionable
exceptions. The tool trims and stores Name/Path without validating non-empty/identifier legality
and then uses name verbatim in generated code while always emitting a 5-quote raw string
delimiter, which can break code generation/compilation with unclear downstream errors.

dotnet/private/GenerateResourcesTool.cs[28-37]
dotnet/private/GenerateResourcesTool.cs[57-65]
Best Practice: Learned patterns

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The resource generator accepts `--input IDENT=path` but does not validate that `IDENT` is a legal C# identifier (or even non-empty after trimming), and it always emits a 5-quote raw string delimiter regardless of file content. This can produce invalid generated C# and fail later with unclear errors.

## Issue Context
This tool is invoked by Bazel (`generated_resource_utilities`), so failures should be fast and actionable (clear stderr message + nonzero exit) when inputs are invalid.

## Fix Focus Areas
- dotnet/private/GenerateResourcesTool.cs[28-37]
- dotnet/private/GenerateResourcesTool.cs[57-65]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

2. Extra newline in resources 🐞 Bug ≡ Correctness
Description
GenerateResourcesTool uses StringBuilder.AppendLine(content), which always appends an extra newline
after each resource, so ResourceUtilities.* constants will not match the input file contents
exactly. These constants are executed/parsed at runtime (JS evaluation and JSON parsing), so this
silent content drift can change behavior and makes byte-for-byte resource embedding impossible.
Code

dotnet/private/GenerateResourcesTool.cs[R59-65]

+    var content = File.ReadAllText(path, Encoding.UTF8);
+    // Use a C# raw string literal with five quotes.
+    // The content must start on a new line and the closing quotes
+    // must be on their own line as well.
+    sb.AppendLine($"    internal const string {name} = {new string('"', 5)}");
+    sb.AppendLine(content);
+    sb.AppendLine($"{new string('"', 5)};");
Evidence
The generator reads each file and then emits it into a raw string literal using AppendLine(content),
which guarantees at least one extra newline beyond the file’s own trailing newline (if any). The
generated constants are then used as executable JavaScript and parsed JSON at runtime, meaning the
embedded string content matters.

dotnet/private/GenerateResourcesTool.cs[57-66]
dotnet/src/webdriver/JavaScriptEngine.cs[136-144]
dotnet/src/webdriver/Firefox/FirefoxProfile.cs[292-300]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`dotnet/private/GenerateResourcesTool.cs` uses `sb.AppendLine(content)` when emitting raw string literals. `AppendLine` always appends an additional newline after `content`, so every embedded resource gets an extra trailing newline (and resources that already end with a newline will effectively gain a blank line).

### Issue Context
These embedded strings are used at runtime as JavaScript sources and as JSON content; the generator should ideally preserve the resource text exactly (or at least avoid adding extra, unintended newlines).

### Fix Focus Areas
- dotnet/private/GenerateResourcesTool.cs[57-66]

### Suggested approach
- Replace `sb.AppendLine(content);` with `sb.Append(content);`
- Ensure the raw string closing delimiter still starts on a new line:
 - If `content` does **not** end with `\n` (or `\r\n`), append a single newline once (e.g., `sb.AppendLine();`).
 - If it already ends with a newline, do not add another.
- Keep the closing delimiter emission unchanged.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Migrates the .NET resource generation utility used by Bazel/MSBuild from a Python script to a C# console tool, removing the Python dependency from this part of the .NET build pipeline.

Changes:

  • Replaced generate_resources_tool.py with a new C# implementation (GenerateResourcesTool.cs) that emits ResourceUtilities.g.cs.
  • Updated Bazel rule documentation to reflect the C# tool invocation.
  • Updated dotnet/private/BUILD.bazel to build the generator as a csharp_binary targeting net10.0.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
dotnet/private/generate_resources_tool.py Removes the prior Python-based generator implementation.
dotnet/private/generate_resources.bzl Updates rule/module documentation to reference the new C# tool.
dotnet/private/GenerateResourcesTool.cs Adds new C# generator tool that reads JS inputs and emits ResourceUtilities constants.
dotnet/private/BUILD.bazel Switches generator target from py_binary to csharp_binary and sets net10.0.

Comment thread dotnet/private/GenerateResourcesTool.cs
Comment thread dotnet/private/GenerateResourcesTool.cs
Comment thread dotnet/private/GenerateResourcesTool.cs
Comment thread dotnet/private/GenerateResourcesTool.cs
Comment thread dotnet/private/GenerateResourcesTool.cs
Comment thread dotnet/private/GenerateResourcesTool.cs
@nvborisenko nvborisenko merged commit ff8a580 into SeleniumHQ:trunk Apr 25, 2026
18 of 19 checks passed
@nvborisenko nvborisenko deleted the dotnet-build-resource-generator branch April 25, 2026 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-build Includes scripting, bazel and CI integrations C-dotnet .NET Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants