Skip to content

Stop mixing Rake::DSL into Object; use Rake::Task.define_task - #8

Open
zev wants to merge 3 commits into
jruby:masterfrom
zev:rake-dsl-no-toplevel-include
Open

zev wants to merge 3 commits into
jruby:masterfrom
zev:rake-dsl-no-toplevel-include

Conversation

@zev

@zev zev commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1.

Rake no longer defines its DSL at the top level outside a Rakefile. This gem worked around that with a global include Rake::DSL, which leaks task, file, sh, etc. into every object, the thing modern Rake removed on purpose.

  • ant_import and ant_task now call Rake::Task.define_task instead of the bare task.
  • Removed require 'rake/dsl_definition' and the global include Rake::DSL.
  • test_rake.rb no longer includes Rake::DSL.

ant, ant_import and ant_task remain top-level methods, so Rakefiles are unaffected. Scripts that relied on this gem leaking task/sh into the top level will need include Rake::DSL themselves, so this probably deserves a minor version bump.

Tested on JRuby with Ant 1.10.18: 30 runs, 0 failures (1 skip: ANT_HOME unset). Also ran a real Rakefile using ant_import and ant_task, and confirmed Object.new.respond_to?(:task, true) is false after require 'ant'.

Stacked on #7: this branch includes #7's commit (its head is in a fork, so it can't be the PR base). Once #7 merges, that commit drops out of this diff. ant_graph_task now also uses Rake::Task.define_task and test_graph.rb no longer includes Rake::DSL, so no follow-up is needed. Full suite with both: 33 runs, 0 failures (1 skip: ANT_HOME unset).

🤖 Generated with Claude Code

zev and others added 3 commits October 1, 2026 20:13
JRuby's rakelib/graph.rake (graph:viz, added in 2011 as an "ant target
visualizer") drew the targets of JRuby's Ant build.xml with Graphviz.
JRuby no longer has an Ant build and removes it in jruby/jruby#9761, so
move the idea here as a reusable feature:

- Rake::Ant.dot(project) returns Graphviz DOT text with an edge from
  each dependency to the target that depends on it, sanitizing - and .
  in target names as graph.rake did
- ant_graph_task(name, buildfile, output) defines a task that
  ant_imports the build file and pipes the DOT text to `dot -Tpng -x`
- CI installs Graphviz so the PNG test runs; it skips without `dot`

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rake no longer exposes its DSL at the top level outside a Rakefile, so the
global include was both unnecessary and the thing modern Rake removed on
purpose. ant_import and ant_task now define tasks via Rake::Task.define_task.
Fixes jruby#1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…m its test

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@zev
zev force-pushed the rake-dsl-no-toplevel-include branch from 2982fd8 to 51727e6 Compare October 2, 2026 16:23
@zev

zev commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Upgrade notes for existing users

Who is affected: only code that is not a Rakefile but relied on require 'ant' (or rake/ant) making Rake's DSL methods available everywhere. Previous versions did a global include Rake::DSL, which put task, file, directory, namespace, rule, desc, sh, multitask, etc. on every object. That include is gone.

Not affected: Rakefiles. Rake already provides its DSL to a Rakefile's main, and ant, ant_import, ant_task (and ant_graph_task from #7) are still top-level methods that work as before:

# Rakefile: no changes needed
require 'ant'
ant_import
ant_task :compile => :prep do
  javac :srcdir => 'src', :destdir => 'build'
end

Symptom if you are affected: NoMethodError: undefined method 'task' for main:Object (or file, sh, namespace, ...) after upgrading. This occurs in plain Ruby scripts, library code, classes or tests that call those methods directly after requiring this gem.

How to fix it (any one of these):

# 1. Opt in to the DSL where you use it (what this gem used to do globally)
require 'ant'
include Rake::DSL        # at top level of a script
# or inside a class/module:
class MyBuild
  include Rake::DSL
end

# 2. Extend a single object instead of polluting everything
builder = Object.new.extend(Rake::DSL)

# 3. Skip the DSL and call Rake's API directly
Rake::Task.define_task(:compile => :prep) { ... }
Rake::FileTask.define_task('out.jar' => 'src') { ... }

Option 1 is the minimal change for an existing script. Options 2 and 3 are preferred for new code because they do not leak methods into every object, which is what current Rake itself avoids.

Because this is a behaviour change for non-Rakefile users, I'd suggest a minor version bump and a changelog line when releasing.

@zev

zev commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@headius let me know if there is anything more here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use of top-level methods from Rake is deprecated

1 participant