Giter VIP home page Giter VIP logo

Comments (7)

sureshjoshi avatar sureshjoshi commented on May 27, 2024

I don't know if this works as any of the tailor stuff I run is pretty basic, but would something like this work for you?

I'm not entirely sure if fmt necessarily runs after tailor, but even if not, creating the new build files should tell fmt that the filesystem has changed and kick it off anyways.

backend_packages = [
    "pants.backend.build_files.fmt.buildifier",
    ...

pants tailor fmt ::

As a fallback, if that didn't work, then maybe something like pants tailor :: && pants fmt ::

https://www.pantsbuild.org/2.20/reference/subsystems/buildifier

from pants.

ahyatt-continua avatar ahyatt-continua commented on May 27, 2024

Yes, those work, and are a way to avoid manually running buildifier, but in exchange you manually run pants tailor. It seems reasonable that anything generated by pants should then be formatted by pants. In other words, it should be that this is something we shouldn't have to think about.

from pants.

sureshjoshi avatar sureshjoshi commented on May 27, 2024

I'll concede, I'm a bit confused as to your workflow now.

support running an auto-format binary after pants tailor

by default, pants tailor creates BUILD files that do not conform to this binary, so now users have to run pants tailor and then manually go and run buildifier.

How are you running pants tailor if not manually? And what's the incremental effort to running pants tailor fmt?

from pants.

ahyatt-continua avatar ahyatt-continua commented on May 27, 2024

Yes, we're running it manually. I'd prefer to just run one command, otherwise we need to mention to everyone the "proper" way to create new build files, which is not just to run pants tailor but to do other things as well. This feature request would simplify that.

from pants.

sureshjoshi avatar sureshjoshi commented on May 27, 2024

So, in pants it's pretty typical to chain goals as a single command:
pants fix fmt lint check ::

That's a very common paradigm, so pants tailor fmt :: would be a single command running 2 goals against all targets.

If you wanted to "simplify" it, you could create an alias to do that for you (https://www.pantsbuild.org/2.20/reference/subsystems/cli#alias).

[cli.alias]
ftailor = "tailor fmt"

from pants.

ahyatt-continua avatar ahyatt-continua commented on May 27, 2024

Thanks, I agree that's better than I had originally come up with.

I still think, though, that automatically formatting (if Pants knows how) anything Pants generates would be a completely reasonable thing to implement, and would help simplify this. Ideally, Pants should get things right without workarounds, even simple ones.

from pants.

huonw avatar huonw commented on May 27, 2024

I agree with your point that this could be better: pants is writing new data to file that it (a) knows how to reformat, and (b) knows should be reformatted. Would be a nice quality-of-life improvement to do the reformatting automatically!

It looks like this is the rule that does the editing, including reading the existing contents:

@rule(desc="Edit BUILD files with new targets", level=LogLevel.DEBUG)
async def edit_build_files(
req: EditBuildFilesRequest, tailor_subsystem: TailorSubsystem
) -> EditedBuildFiles:
ptgts_by_build_file = group_by_build_file(
tailor_subsystem.build_file_name, req.putative_targets
)
# There may be an existing *directory* whose name collides with that of a BUILD file
# we want to create. This is more likely on a system with case-insensitive paths,
# such as MacOS. We detect such cases and use an alt BUILD file name to fix.
existing_paths = await Get(Paths, PathGlobs(ptgts_by_build_file.keys()))
existing_dirs = set(existing_paths.dirs)
# Technically there could be a dir named "BUILD.pants" as well, but that's pretty unlikely.
ptgts_by_build_file = {
(f"{bf}.pants" if bf in existing_dirs else bf): pts
for bf, pts in ptgts_by_build_file.items()
}
existing_build_files_contents = await Get(DigestContents, PathGlobs(ptgts_by_build_file.keys()))
existing_build_files_contents_by_path = {
ebfc.path: ebfc.content for ebfc in existing_build_files_contents
}
def make_content(bf_path: str, pts: Iterable[PutativeTarget]) -> FileContent:
existing_content_bytes = existing_build_files_contents_by_path.get(bf_path)
existing_content = (
tailor_subsystem.build_file_header
if existing_content_bytes is None
else existing_content_bytes.decode()
)
new_content_bytes = make_content_str(
existing_content, tailor_subsystem.build_file_indent, pts
).encode()
return FileContent(bf_path, new_content_bytes)
new_digest = await Get(
Digest,
CreateDigest([make_content(path, ptgts) for path, ptgts in ptgts_by_build_file.items()]),
)
updated = set(existing_build_files_contents_by_path.keys())
created = set(ptgts_by_build_file.keys()) - updated
return EditedBuildFiles(new_digest, tuple(sorted(created)), tuple(sorted(updated)))

I'm not exactly sure how we'd apply the appropriate build file formatting rules likely something involving rules from https://github.com/pantsbuild/pants/blob/0d35991d0814fa4f45ce504933d143525898e653/src/python/pants/core/goals/update_build_files.py

from pants.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.