Skip to content

Reduce duplication in CmdRun - #4607

Merged
fgaz merged 3 commits into
haskell:masterfrom
fgaz:new-run/reduce-duplication
Jul 20, 2017
Merged

fgaz merged 3 commits into
haskell:masterfrom
fgaz:new-run/reduce-duplication

Conversation

@fgaz

@fgaz fgaz commented Jul 17, 2017

Copy link
Copy Markdown
Member

followup of #4586

Previously, the extraction of the target from the targetStrings was done
twice: inside the function passed to 'runProjectPreBuildPhase' and right
after it.

Now 'runProjectPreBuildPhase' is able to return information from that
function, and the duplication was removed, along with some assumptions
about the number of targets.


This is one of the possible approaches, the other being adding a field in ProjectBuildContext.
The only way to avoid a Maybe or a sum type in said field, is parameterizing it.

data ProjectBuildContext arbitraryData = ProjectBuildContext{
  ...
  someFieldName :: arbitraryData
}

Which has the advantage of not having to modify all the Cmd*.hs files and passing ()s everywhere, but after some thinking I like the first solution the best.

@fgaz
fgaz requested a review from dmwit July 17, 2017 14:25
-- ^ A function that selects a subset of the plan and returns it,
-- It can also return some arbitrary data, which can be useful in case
-- the caller needs it.
-> (ElaboratedInstallPlan -> IO (ElaboratedInstallPlan, a))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you need polymorphism here if you're only ever using this value in monomorphic context -- it's either ignored (()) or (UnitId, ComponentId)? IMO it makes more sense to stick a (UnitId, ComponentId) member into the ProjectBuildStatus record.

@dmwit dmwit Jul 17, 2017 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't have a strong preference on "return directly" vs. "stuff it in the ProjectBuildStatus", but for polymorphic vs. monomorphic, the polymorphic version is nice for forward-compatibility, e.g. if future consumers want some other information out of the pre-build phase target selection process.

If we stick with "return directly", perhaps there should be, like, a runProjectPreBuildPhase_ f = fst <$> runProjectPreBuildPhase (\x -> (f x, ()) or something like that which has the old type. Then existing uses would be a bit less noisy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well, if I add another field it has to be Maybe (UnitId, ComponentId) or equivalent, because that record is used in other commands too. But then I'd have to unwrap that Maybe and rely on the assumption that I set it to Just in preBuild.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, right, a wrapper.
Then the only question is whether to add the _ to the wrapper or the wrapped. In base it's the wrapper I think

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm wary of over-abstracting the code prematurely for the sake of future consumers that may never materialise. If they do, refactoring is easy in Haskell. Existing type is nice because there's only one place you need to look at to see the data that's produced in the pre-build step.

But then I'd have to unwrap that Maybe and rely on the assumption that I set it to Just in preBuild.

That's okay IMO, just make it a fatal error if you get Nothing.

@23Skidoo 23Skidoo Jul 17, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

BTW, it probably makes sense to you to tackle new-bench and new-test (i.e. #3638) after this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Isn't new-test already done, according to that issue (also, it works for me)?

Or we have to redo it, since the current one relies on the "old" Setup, as most of the new-commands do (I think. They seem to do so.)?

new-ench builds but does not run the benchmark, same as run before my pr.

@23Skidoo 23Skidoo Jul 18, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, you're right, new-test is implemented using the setup wrapper because it needs to run hooks and we have a test command in the setup interface. new-bench can have custom hooks attached too and should also work this way.

new-test seems to be confused by non-trivial project structure, though, so it still makes sense to look at it:

~/code/haskell/cabal[master]$ cabal new-test                    
cabal: No targets given and there is no package in the current directory. Use
the target 'all' for all packages in the project or specify packages or
components by name or location. See 'cabal build --help' for more details on
target options.

~/code/haskell/cabal[master]$ cabal new-test cabal-install
cabal: Cannot test the package cabal-install-2.1.0.0 because none of the
components are available to build: the test suite 'unit-tests', the test suite
'solver-quickcheck', the test suite 'memory-usage-tests' and the test suite
'integration-tests2' are all marked as 'buildable: False'

~/code/haskell/cabal[master]$ cabal new-test cabal-install:unit-tests               
cabal: Cannot run the test suite 'unit-tests' because it is marked as
'buildable: False' within the 'cabal-install.cabal' file (at least for the
current configuration). If you believe it should be buildable then check the
.cabal file to see if the buildable property is conditional on flags.
Alternatively you may simply have to edit the .cabal file to declare it as
buildable and fix any resulting build problems.

~/code/haskell/cabal[master]$ cabal new-test cabal-install:unit-tests --enable-tests
cabal: Cannot run the test suite 'unit-tests' because it is marked as
'buildable: False' within the 'cabal-install.cabal' file (at least for the
current configuration). If you believe it should be buildable then check the
.cabal file to see if the buildable property is conditional on flags.
Alternatively you may simply have to edit the .cabal file to declare it as
buildable and fix any resulting build problems.

~/code/haskell/cabal[master]$ cabal new-configure --enable-tests   
'cabal.project.local' file already exists. Now overwriting it.
In order, the following would be built (use -v for more details):
Assertion failed
CallStack (from HasCallStack):
  assert, called at ./Distribution/Client/ProjectPlanning.hs:280:5 in main:Distribution.Client.ProjectPlanning

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Anyhow, I still think that sticking TargetMap into ProjectBuildContext makes sense.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, so I'll fall back to the one-element assumption, but at least there isn't much duplication now.

I still think the polymorphic solution is cleaner-ish, but I'll proceed with your solution if the others don't chime in in a couple of days

@fgaz
fgaz force-pushed the new-run/reduce-duplication branch 2 times, most recently from df5b7c2 to 4bccd3e Compare July 17, 2017 15:51
@ezyang

ezyang commented Jul 19, 2017

Copy link
Copy Markdown
Contributor

Hey @fgaz, do you need me to look at this commit? It looks like a bunch of people commented already and I don't want to pile on. Let me know.

@fgaz

fgaz commented Jul 19, 2017

Copy link
Copy Markdown
Member Author

@ezyang
Just wanted a third opinion on polymorphism (see above), since @dmwit and @23Skidoo have opposite ones and I don't trust myself enough to make the final decision.

(P.S.: I replied by email some hours ago but my comment didn't show up here. Just a heads up in case this happens to any of you)

@ezyang

ezyang commented Jul 20, 2017

Copy link
Copy Markdown
Contributor

Let me preface this by saying, it doesn't matter, ship something and be willing to refactor it later if it needs to be done differently.

That being said, let me be philosophical for a moment. The crux of the problem is that the logical interpretation of runProjectPreBuildPhase returning a ProjectBuildContext (which represents the "result" of pre-build") is in conflict with the fact that, from an implementation perspective, the easiest place to say what targets are selected are during the pruning process.

I think the approach which is most faithful to the original intent of the code is to refine (ElaboratedInstallPlan -> IO ElaboratedInstallPlan) to not ONLY return the pruned ElaboratedInstallPlan, but also a description of how it was pruned, which, incidentally, is going to contain the target information you are interested in here.

@23Skidoo

Copy link
Copy Markdown
Member

Needs to be rebased.

Previously, the extraction of the target from the targetStrings was done
twice: inside the function passed to 'runProjectPreBuildPhase' and right
after it.

Now 'runProjectPreBuildPhase' is able to return information from that
function, and the duplication was removed, along with some assumptions
about the number of targets.
@fgaz
fgaz force-pushed the new-run/reduce-duplication branch from 863be56 to 484bb6c Compare July 20, 2017 17:34
Return a monomorphic TargetsMap from selectPlanSubset and put it in the
final ProjectBuildContext.

This causes slight duplication in CmdRun.hs, but not as bas as when we
didn't return anything. In exchange, the final return type is much
simpler.

Note that CmdConfigure returns an empty TargetsMap, as configure doesn't
accept targets.
@fgaz

fgaz commented Jul 20, 2017

Copy link
Copy Markdown
Member Author

I'm going to merge once it's green

@fgaz
fgaz force-pushed the new-run/reduce-duplication branch from 80c698e to 8ede238 Compare July 20, 2017 19:11
@fgaz
fgaz merged commit 76262fc into haskell:master Jul 20, 2017
@23Skidoo

Copy link
Copy Markdown
Member

@fgaz Thanks! Very nice to be able to use new-run at last.

@fgaz fgaz added the cabal-install: v2-build system Affecting v2-build and related commands that use v2-architecture (aka "nix local builds"). label Jul 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cabal-install: v2-build system Affecting v2-build and related commands that use v2-architecture (aka "nix local builds").

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants