Make WordPress Core

Opened 6 months ago

Last modified 2 days ago

#65279 new enhancement

Prevent non css files from being placed in CSS folders and prevent non JS files from being placed in the JS folders

Reported by: jorbin Owned by:
Priority: normal Milestone: 7.2
Component: Build/Test Tools Version:
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

Currently, it's possible to place php files in /wp-includes/css, wp-includes/js, wp-admin/js, and wp-admin/css. These folders should only ever contain their respective file types and these file types should only ever be in these folders. And automated check should be run to enforce this.

Change History (25)

#1 @desrosj
6 months ago

Related #65278.

Last edited 6 months ago by desrosj (previous) (diff)

This ticket was mentioned in ​Slack in #core by desrosj. ​View the logs.


6 months ago

This ticket was mentioned in ​PR #11915 on ​WordPress/wordpress-develop by ​@mohamedahamed.


6 months ago
#3

  • Keywords has-patch added

## Summary

Adds automated enforcement of file type restrictions in CSS and JS source directories. Currently, it's possible to place files with incorrect extensions (e.g., .php files) in src/wp-includes/css/, src/wp-includes/js/, src/wp-admin/css/, and src/wp-admin/js/ without any automated check catching it.

This PR introduces two-directional enforcement:

### Inward Check

"These folders should only ever contain their respective file types"

Verifies that CSS directories only contain .css files and JS directories only contain .js files. Gitignored build artifacts (e.g., registry.php, *.asset.php, TinyMCE skins) are handled via an allow-list in the Grunt task only — the GitHub Actions workflow operates on a clean checkout and never encounters them.

### Outward Check

"These file types should only ever be in these folders"

Verifies that .css and .js files do not exist outside their designated css/ and js/ directories within wp-includes/ and wp-admin/. Excluded directories that legitimately contain CSS/JS files:

  • wp-includes/blocks/ — Gutenberg block-specific assets (gitignored).
  • wp-includes/build/ — Routes/pages build system JS files (tracked).

## Changes

### Gruntfile.js

  • Adds a verify:file-types Grunt task with both inward and outward checks.
  • Wired into the existing verify:build composite task (runs during grunt build).
  • Allow-list for gitignored build artifacts that exist locally after build:dev (TinyMCE skins/fonts/images, *.asset.php manifests, registry.php, CodeMirror/Jcrop/mediaelement/thickbox/imgAreaSelect bundled assets, SCSS color scheme sources, license files).

### .github/workflows/file-type-check.yml (New)

  • Lightweight GitHub Actions workflow using shell find commands (no npm ci required).
  • Triggers on pull requests and pushes when files change in the target directories or when CSS/JS files are added anywhere under wp-includes/ or wp-admin/.
  • No allow-list for gitignored files needed — checkout only contains tracked files.
  • Follows existing WordPress workflow conventions: pinned action SHAs, concurrency groups, permission scoping, repository guard, slack notifications, and failed-workflow dispatch.

## Testing

Test Result
npx grunt jshint:grunt ✅ Lint free
npx grunt verify:file-types on clean codebase ✅ Passed
Inward violation: .php file in src/wp-includes/css/ ✅ Detected
Outward violation: .css file in src/wp-admin/includes/ ✅ Detected

Trac ticket: https://core.trac.wordpress.org/ticket/65279

## Use of AI Tools

AI assistance: Yes
Tool(s): Gemini (Antigravity IDE)
Model(s): Claude Opus 4.6 (Thinking)
Used for: Initial code skeleton generation for the Grunt task and GitHub Actions workflow; codebase analysis to identify all gitignored vs tracked files requiring allow-listing; iterative refinement based on review feedback. Final implementation and testing were reviewed and validated by me.

This ticket was mentioned in ​Slack in #core by adrianduffell. ​View the logs.


4 months ago

#5 @adrianduffell
4 months ago

With the 7.1 beta approaching, this has a chance to be included if the PR can be reviewed in the coming days.

This ticket was mentioned in ​Slack in #core by adrianduffell. ​View the logs.


4 months ago

#7 @adrianduffell
4 months ago

  • Milestone 7.1 → Future Release

This was discussed in today’s bug scrub. Unfortunately, with the 7.1 beta release just hours away, I will punt this. It looks valuable to pick up in 7.2.

​@lancewillett commented on ​PR #11915:


8 weeks ago
#8

@adimoldovan Could you please take a look here, both the approach and the code?

This ticket was mentioned in ​PR #13684 on ​WordPress/wordpress-develop by ​@adrianmoldovanwp.


8 weeks ago
#9

Adds a workflow that checks CSS and JS files sit in the directories meant for them.

This is an alternative to #11915, which does the same job by walking the filesystem.

How it works

It asks Git which files are tracked:

git -c core.quotePath=false ls-files -- src/wp-includes src/wp-admin

Build output is gitignored, so it drops out by construction and needs no allow-list. Two exclusions remain, and both are genuinely tracked files:

  • the SCSS sources for the admin color schemes, under src/wp-admin/css/colors/
  • the generated routes and pages build system, under src/wp-includes/build/

Only committed files can break the repository for other people. A stray file in your own working copy harms nobody. This checks exactly the set that can cause the problem.

Two checks run:

  1. Inward — the CSS directories hold only .css, and the JS directories hold only .js.
  2. Outward — no .css or .js sits outside those directories.
#11915 this
Files examined everything on disk tracked files
Allow-list ~50 patterns, hand-maintained 2 exclusions
Local vs CI result differs by build state identical
Implementations 2 (JS and shell) 1
Lines 438 152
Dependencies glob, minimatch (neither declared) git

Testing instructions

The workflow passes on trunk today. To see it fail, stage a misplaced file and run the checks by hand:

# Each of these should be reported.
touch src/wp-includes/css/oops.php src/wp-admin/includes/stray.js
git add -fN src/wp-includes/css/oops.php src/wp-admin/includes/stray.js

tracked=$( git -c core.quotePath=false ls-files -- src/wp-includes src/wp-admin )
printf '%s\n' "$tracked" | grep -E '^src/(wp-includes|wp-admin)/css/' | grep -vE '\.css$' | grep -vE '^src/wp-admin/css/colors/.+\.scss$'
printf '%s\n' "$tracked" | grep -E '\.(css|js)$' | grep -vE '^src/(wp-includes|wp-admin)/(css|js)/' | grep -v '^src/wp-includes/build/'

# Clean up.
git rm -q --cached --force src/wp-includes/css/oops.php src/wp-admin/includes/stray.js
rm src/wp-includes/css/oops.php src/wp-admin/includes/stray.js

An unstaged copy of the same files reports nothing, which is the intended behavior.

Linting:

actionlint .github/workflows/file-type-check.yml

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Drafted the workflow and adversarial reviews.

​@jorbin commented on ​PR #13684:


8 weeks ago
#10

This looks like a good start! I think it might also be valuable to check after a build since some files are copied over from npm packages and gutenberg.

​@lancewillett commented on ​PR #11915:


8 weeks ago
#11

Thank you, @NoumaanAhamed, for working on this and helping move the directory checks forward. We’re continuing with #13684 as the simpler foundation, so I’m closing this PR in favor of that implementation.

There is still work to do before landing, especially checking the final build output. Your work on the Grunt verification is useful context for that discussion. You’re very welcome to help review and test #13684, particularly the generated-file coverage and necessary exceptions, and continue contributing to the final solution.

Thanks again for your contribution!

This ticket was mentioned in ​PR #13706 on ​WordPress/wordpress-develop by ​@adrianmoldovanwp.


8 weeks ago
#12

Adds a verify:file-types Grunt task. It checks that the CSS and JS files in build/ sit in the directories meant for them. It follows up on ​this comment on #13684: the build copies files from npm packages and Gutenberg, and a check of tracked files cannot see them.

The two checks answer different questions:

#13684 this PR
Question Did someone commit a file in the wrong place? Did a build task write a file to the wrong place?
Input tracked files in src/ build/ after a production build
Runs every PR where npm run build runs

Neither PR depends on the other.

How it works

The task runs in verify:build, next to verify:old-files and verify:source-maps. So it runs at the end of every npm run build, which CI already runs in reusable-test-core-build-process.yml before it zips build/. It needs no new workflow. It does not run in npm run build:dev.

It flags three cases under build/wp-admin/ and build/wp-includes/:

  • a non-CSS file in a css/ directory
  • a non-JS file in a js/ directory
  • a CSS or JS file outside those directories

An allow-list of 12 patterns covers the known exceptions. I measured it against a full production build (npm ci, npm run gutenberg:download, npm run build, 5,016 files). It covers all 99 exceptions inside css/ and js/ directories, and all 640 CSS and JS files outside them. Those 640 are all in wp-includes/blocks/ and wp-includes/build/.

#11915 also checks after a build, but it checks src/ after build:dev and needs about 50 patterns. build/ is what ships, and it needs fewer patterns.

<details>
<summary>Worth a separate ticket: the build ships SCSS sources</summary>

The production build copies 12 .scss files into build/wp-admin/css/colors/, so WordPress releases contain them. The allow-list permits them for now.

</details>

Testing instructions

npm ci
npm run gutenberg:download
npm run build

The build passes. To see the task fail, add misplaced files and run it again:

touch build/wp-admin/stray.css build/wp-includes/js/stray.txt build/wp-includes/js/.eslintrc
npm run grunt verify:file-types

It reports all three files. Clean up:

rm build/wp-admin/stray.css build/wp-includes/js/stray.txt build/wp-includes/js/.eslintrc

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Drafted the task, measured the allow-list against a real build, and ran code reviews. I reviewed the result.

​@adrianmoldovanwp commented on ​PR #13684:


8 weeks ago
#13

  1. Validate the final build output.

Build it in a separate PR #13706. This PR keeps the tracked-source check.

  1. Handle filenames without line-based parsing.

Fixed in 784b426e5a, but not with -z. The checks now pass pathspec exclusions to git ls-files, so Git does the filtering and no path is matched as text. Command substitution drops NUL bytes, so -z needs a temp file or mapfile, and mapfile loses the set -e check on Git's exit status. I tested names with a tab and a newline, both in the right place and the wrong one.

  1. Document narrow exceptions for legitimate generated files.

This check sees only tracked files. Its two exceptions are both tracked: the admin color scheme SCSS and src/wp-includes/build/. 455cbde769 adds a comment that the generated PHP in css/dist and js/dist is missing only because it is untracked, and points to #65278. If that PHP becomes tracked, this check will flag it. The allow-list for the built copies is in #13706, and I will mark its PHP entries as provisional there.

​@lancewillett commented on ​PR #13684:


8 weeks ago
#14

The split into tracked-source and final-build checks addresses the earlier scope concern, and Git-side filtering avoids the filename parsing problem. There is still a blocker in the inward exclusions.

In a fresh repository, I force-added src/wp-admin/js/base.js and src/wp-admin/js/nested/oops.css. Both appear in git ls-files --stage, but this command prints nothing:

git ls-files -- src/wp-includes/js src/wp-admin/js ':(exclude)*.js'

The CSS check likewise misses a tracked not-allowed.scss alongside a valid CSS file. I reproduced the exclusion issue on Linux too.

Please use directory-scoped glob exclusions:

inward_css=$( git ls-files -- src/wp-includes/css src/wp-admin/css \
  ':(exclude,glob)src/wp-includes/css/**/*.css' \
  ':(exclude,glob)src/wp-admin/css/**/*.css' \
  ':(exclude,glob)src/wp-admin/css/colors/**/*.scss' )

inward_js=$( git ls-files -- src/wp-includes/js src/wp-admin/js \
  ':(exclude,glob)src/wp-includes/js/**/*.js' \
  ':(exclude,glob)src/wp-admin/js/**/*.js' )

This form passed fixtures covering ordinary misplaced files, tabs/newlines, valid CSS/JS, and the narrow SCSS exception. It also passed against the current Core tree. Please update the matching commands in the PR description too.

---
<sub>Adversarial review · <code>gpt-6</code></sub>

​@lancewillett commented on ​PR #13684:


8 weeks ago
#15

The split into tracked-source and final-build checks addresses the earlier scope concern, and Git-side filtering avoids the filename parsing problem. There is still a blocker in the inward exclusions.

In a fresh repository, I force-added src/wp-admin/js/base.js and src/wp-admin/js/nested/oops.css. Both appear in git ls-files --stage, but this command prints nothing:

git ls-files -- src/wp-includes/js src/wp-admin/js ':(exclude)*.js'

The CSS check likewise misses a tracked not-allowed.scss alongside a valid CSS file. I reproduced the exclusion issue on Linux too.

Please use directory-scoped glob exclusions:

inward_css=$( git ls-files -- src/wp-includes/css src/wp-admin/css \
  ':(exclude,glob)src/wp-includes/css/**/*.css' \
  ':(exclude,glob)src/wp-admin/css/**/*.css' \
  ':(exclude,glob)src/wp-admin/css/colors/**/*.scss' )

inward_js=$( git ls-files -- src/wp-includes/js src/wp-admin/js \
  ':(exclude,glob)src/wp-includes/js/**/*.js' \
  ':(exclude,glob)src/wp-admin/js/**/*.js' )

This form passed fixtures covering ordinary misplaced files, tabs/newlines, valid CSS/JS, and the narrow SCSS exception. It also passed against the current Core tree. Please update the matching commands in the PR description too.

---
<sub>Adversarial review · <code>gpt-6</code></sub>

#16 @lancewillett
8 weeks ago

In 63917:

Build/Test Tools: Check file types in the build directory.

Check the final production build for misplaced CSS and JavaScript files and unexpected file types in their directories. Run the check alongside the existing build verification tasks.

Allow known bundled assets and mark the generated PHP exceptions as provisional while their placement remains under discussion.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13706

Props adrianmoldovanwp.
See #65279.

#18 @lancewillett
8 weeks ago

In 63918:

Build/Test Tools: Revert the build file-type check.

Revert [63917] pending agreement on the placement of generated PHP files and the exceptions allowed by the check.

See #65279, #65278.

​@lancewillett commented on ​PR #13706:


8 weeks ago
#19

Reopening after the revert in https://core.trac.wordpress.org/changeset/63918.

Next step: settle the file-placement exceptions and upstream formatting approach before moving these forward.

Discussion: ​https://wordpress.slack.com/archives/C08D0Q6BHNY/p1790267518339219

​@adrianmoldovanwp commented on ​PR #13684:


7 weeks ago
#20

The split into tracked-source and final-build checks addresses the earlier scope concern, and Git-side filtering avoids the filename parsing problem. There is still a blocker in the inward exclusions.

Fixed. 98740e8eaf uses your directory-anchored form for both inward checks, and anchors the outward check the same way.
I tested it against 23 fixture files, including tabs and newlines in names, nested files, dotfiles, the SCSS exception and build/. All 11 misplaced files are reported and none of the correctly placed ones are. The earlier version missed 4. It also passes on the current tree and on 6.8 to 7.1.

I've updated the commands in the PR description.

​@lancewillett commented on ​PR #13684:


7 weeks ago
#21

@aaronjorbin, the earlier pathspec issue is fixed. Local fixture checks and actionlint pass, and CI is green.

Are you comfortable landing this tracked-source check independently, with the documented SCSS and wp-includes/build/ exceptions? It does not check generated output or settle the generated PHP placement discussion in #65278/#65279. I’m holding landing pending agreement on that scope.

---
<sub>Adversarial review · <code>gpt-6</code></sub>

​@lancewillett commented on ​PR #13684:


2 weeks ago
#22

@aaronjorbin @johnbillion @desrosj, following up on the remaining scope decision: can the tracked-source check land independently with the documented SCSS and wp-includes/build/ exceptions, while generated-output checks and PHP placement remain separate? The code-review fixes are addressed; I’m still holding landing for agreement on that boundary.

<sub>AI review · <code>gpt-6</code></sub>

​@lancewillett commented on ​PR #13684:


9 days ago
#23

@aaronjorbin @johnbillion @desrosj following up on my Oct 31 question: can this check stay scoped to tracked source files? That's the only open decision before landing.

<sub>AI review · <code>claude-opus-5-5</code></sub>

​@lancewillett commented on ​PR #13684:


2 days ago
#24

@adimoldovan could you switch runs-on to ubuntu-26.04? https://core.trac.wordpress.org/changeset/65467 moved every other workflow to it today, and registered the label for actionlint.

I re-ran the three checks against current trunk: no violations, and dropping the color-scheme exclude flags all 12 SCSS files, as expected. With the runner updated, this is ready to land.

<sub>AI review · <code>claude-opus-5-5</code></sub>

#25 @lancewillett
2 days ago

  • Milestone Future Release → 7.2
Note: See TracTickets for help on using tickets.