From 891da140b7c87b5cbd192404fb4dbb772ea26a66 Mon Sep 17 00:00:00 2001 From: softworkz Date: Wed, 21 Jan 2026 06:42:16 +0100 Subject: [PATCH] Relax Migration check for package.json in root --- docs/Core/Migration-Checks.md | 35 ++++++-- .../build/ElectronNET.MigrationChecks.targets | 87 +++++++++++++++++-- 2 files changed, 109 insertions(+), 13 deletions(-) diff --git a/docs/Core/Migration-Checks.md b/docs/Core/Migration-Checks.md index cda0ba71..e07902fc 100644 --- a/docs/Core/Migration-Checks.md +++ b/docs/Core/Migration-Checks.md @@ -8,7 +8,9 @@ When you build an Electron.NET project, the following validation checks are perf | Code | Check | Description | |------|-------|-------------| -| [ELECTRON001](#1-packagejson-not-allowed) | package.json not allowed | Ensures no package.json exists outside ElectronHostHook | +| [ELECTRON001](#1-packagejson-rules) | package.json location rules | Ensures `package.json`/`package-lock.json` aren’t present in unsupported locations (root `package.json` handled separately) | +| [ELECTRON008](#1-packagejson-rules) | root package.json contains electron | Warns when root `package.json` contains the word `electron` (case-insensitive) | +| [ELECTRON009](#1-packagejson-rules) | root package.json copied to output | Warns when root `package.json` is configured to be copied to output/publish | | [ELECTRON002](#2-electron-manifestjson-not-allowed) | electron-manifest.json not allowed | Detects deprecated manifest files | | [ELECTRON003](#3-electron-builderjson-location) | electron-builder.json location | Verifies electron-builder.json exists in Properties folder | | [ELECTRON004](#3-electron-builderjson-location) | electron-builder.json wrong location | Warns if electron-builder.json is found in incorrect locations | @@ -18,24 +20,38 @@ When you build an Electron.NET project, the following validation checks are perf --- -## 1. package.json not allowed +## 1. package.json rules -**Warning Code:** `ELECTRON001` +**Warning Codes:** `ELECTRON001`, `ELECTRON008`, `ELECTRON009` ### What is checked -The build system scans for `package.json` and `package-lock.json` files in your project directory. These files should not exist in the project root or subdirectories (with one exception). +The build system scans for `package.json` and `package-lock.json` files in your project directory. + +Rules: + +- **ELECTRON001**: `package.json` / `package-lock.json` must not exist in the project directory or subdirectories + - Exception: `ElectronHostHook` folder is allowed + - Note: a **root** `package.json` is **excluded** from `ELECTRON001` and validated by `ELECTRON008` / `ELECTRON009` + +- **ELECTRON008**: If a root `package.json` exists, it must **not** contain electron-related dependencies or configuration. + +- **ELECTRON009**: If a root `package.json` exists, it must **not** be configured to be copied to output/publish (for example via `CopyToOutputDirectory` / `CopyToPublishDirectory` metadata) ### Why this matters -In previous versions of Electron.NET, a `package.json` file was required in the project. The new version generates this file automatically from MSBuild properties defined in your `.csproj` file. +Electron.NET generates its Electron-related `package.json` during publishing based on MSBuild properties. A user-maintained Electron-related `package.json` can conflict with that process. + +Also, ensuring the root `package.json` is not copied prevents accidentally shipping it with the published app. ### Exception -A `package.json` file **is allowed** in the `ElectronHostHook` folder if you're using custom host hooks. This is the only valid location for a manually maintained package.json. +A `package.json` / `package-lock.json` file **is allowed** in the `ElectronHostHook` folder if you're using custom host hooks. ### How to fix +If you have an Electron-related `package.json` from older Electron.NET versions: + 1. **Open your project's `.csproj` file** 2. **Add the required properties** to a PropertyGroup with the label `ElectronNetCommon`: @@ -51,7 +67,12 @@ A `package.json` file **is allowed** in the `ElectronHostHook` folder if you're ``` -3. **Delete the old `package.json`** file from your project root +3. **Delete** Electron-related `package.json` / `package-lock.json` files (except those under `ElectronHostHook` if applicable) + +If you keep a root `package.json` for non-Electron reasons: + +- Ensure it does **not** contain electron dependencies or configuration (fixes `ELECTRON008`) +- Ensure it is **not** copied to output/publish (fixes `ELECTRON009`) > **See also:** [Migration Guide](Migration-Guide.md) for complete migration instructions. diff --git a/src/ElectronNET/build/ElectronNET.MigrationChecks.targets b/src/ElectronNET/build/ElectronNET.MigrationChecks.targets index df2336fe..666c1785 100644 --- a/src/ElectronNET/build/ElectronNET.MigrationChecks.targets +++ b/src/ElectronNET/build/ElectronNET.MigrationChecks.targets @@ -4,6 +4,8 @@ ElectronCheckNoPackageJson; + ElectronCheckRootPackageJsonNoElectron; + ElectronCheckRootPackageJsonNotCopied; ElectronCheckNoManifestJson; ElectronCheckElectronBuilderJson; ElectronCheckNoParentPaths; @@ -19,14 +21,17 @@ - + <_InvalidPackageJson Include="$(MSBuildProjectDirectory)\**\package.json" - Exclude="$(MSBuildProjectDirectory)\ElectronHostHook\**\package.json; + Exclude="$(MSBuildProjectDirectory)\package.json; + $(MSBuildProjectDirectory)\ElectronHostHook\**\package.json; $(MSBuildProjectDirectory)\bin\**\package.json; $(MSBuildProjectDirectory)\obj\**\package.json; $(MSBuildProjectDirectory)\publish\**\package.json; @@ -46,17 +51,87 @@ +EXCEPTION: +- package.json and package-lock.json files ARE allowed in the 'ElectronHostHook' folder for custom host hook implementations. +- A package.json in the project root is handled by separate migration checks." /> + + + + + + + + <_RootPackageJsonLines Include="$([System.IO.File]::ReadAllLines('$(MSBuildProjectDirectory)\package.json'))" /> + + + + <_RootPackageJsonContent>@(_RootPackageJsonLines, ' ') + <_RootPackageJsonHasElectron>false + <_RootPackageJsonHasElectron Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(_RootPackageJsonContent)', 'electron', System.Text.RegularExpressions.RegexOptions.IgnoreCase))">true + + + + + + + + + + + <_RootPackageJsonFile Include="@(Content);@(None)" + Condition="'%(Identity)' == 'package.json' OR '%(Identity)' == '$(MSBuildProjectDirectory)\package.json'" /> + + + + <_RootPackageJsonIsCopied>false + <_RootPackageJsonIsCopied Condition="'@(_RootPackageJsonFile)' != '' AND ( '%(_RootPackageJsonFile.CopyToOutputDirectory)' != '' OR '%(_RootPackageJsonFile.CopyToPublishDirectory)' != '' )">true + + +