Skip to content

Commit ead52c1

Browse files
authored
Merge pull request #757 from layus/fix-a-lot
Various fixes and bugfixes encountered while testing
2 parents 6bc278c + 2af45ba commit ead52c1

File tree

7 files changed

+59
-46
lines changed

7 files changed

+59
-46
lines changed

src/Nix/Builtins.hs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,19 @@ builtinsList = sequence
145145
version <- toValue (5 :: Int)
146146
pure $ Builtin Normal ("langVersion", version)
147147

148-
, add0 Normal "nixPath" nixPath
149148
, add TopLevel "abort" throw_ -- for now
150149
, add2 Normal "add" add_
151150
, add2 Normal "addErrorContext" addErrorContext
152151
, add2 Normal "all" all_
153152
, add2 Normal "any" any_
153+
, add2 Normal "appendContext" appendContext
154154
, add Normal "attrNames" attrNames
155155
, add Normal "attrValues" attrValues
156156
, add TopLevel "baseNameOf" baseNameOf
157157
, add2 Normal "bitAnd" bitAnd
158158
, add2 Normal "bitOr" bitOr
159159
, add2 Normal "bitXor" bitXor
160+
, add0 Normal "builtins" builtinsBuiltin
160161
, add2 Normal "catAttrs" catAttrs
161162
, add2 Normal "compareVersions" compareVersions_
162163
, add Normal "concatLists" concatLists
@@ -205,15 +206,21 @@ builtinsList = sequence
205206
, add2 Normal "elemAt" elemAt_
206207
, add Normal "exec" exec_
207208
, add0 Normal "false" (pure $ nvConstant $ NBool False)
209+
--, add Normal "fetchGit" fetchGit
210+
--, add Normal "fetchMercurial" fetchMercurial
208211
, add Normal "fetchTarball" fetchTarball
209212
, add Normal "fetchurl" fetchurl
210213
, add2 Normal "filter" filter_
214+
--, add Normal "filterSource" filterSource
215+
, add2 Normal "findFile" findFile_
211216
, add3 Normal "foldl'" foldl'_
212217
, add Normal "fromJSON" fromJSON
218+
--, add Normal "fromTOML" fromTOML
213219
, add Normal "functionArgs" functionArgs
214-
, add2 Normal "genList" genList
215220
, add Normal "genericClosure" genericClosure
221+
, add2 Normal "genList" genList
216222
, add2 Normal "getAttr" getAttr
223+
, add Normal "getContext" getContext
217224
, add Normal "getEnv" getEnv_
218225
, add2 Normal "hasAttr" hasAttr
219226
, add Normal "hasContext" hasContext
@@ -236,14 +243,15 @@ builtinsList = sequence
236243
, add2 TopLevel "mapAttrs" mapAttrs_
237244
, add2 Normal "match" match_
238245
, add2 Normal "mul" mul_
246+
, add0 Normal "nixPath" nixPath
239247
, add0 Normal "null" (pure $ nvConstant NNull)
240248
, add Normal "parseDrvName" parseDrvName
241249
, add2 Normal "partition" partition_
250+
--, add Normal "path" path
242251
, add Normal "pathExists" pathExists_
243252
, add TopLevel "placeholder" placeHolder
244253
, add Normal "readDir" readDir_
245254
, add Normal "readFile" readFile_
246-
, add2 Normal "findFile" findFile_
247255
, add2 TopLevel "removeAttrs" removeAttrs
248256
, add3 Normal "replaceStrings" replaceStrings
249257
, add2 TopLevel "scopedImport" scopedImport
@@ -252,26 +260,25 @@ builtinsList = sequence
252260
, add2 Normal "split" split_
253261
, add Normal "splitVersion" splitVersion_
254262
, add0 Normal "storeDir" (pure $ nvStr $ principledMakeNixStringWithoutContext "/nix/store")
263+
--, add Normal "storePath" storePath
255264
, add' Normal "stringLength" (arity1 $ Text.length . principledStringIgnoreContext)
256265
, add' Normal "sub" (arity2 ((-) @Integer))
257-
, add' Normal "substring" (substring @e @t @f @m)
266+
, add' Normal "substring" substring
258267
, add Normal "tail" tail_
259-
, add0 Normal "true" (pure $ nvConstant $ NBool True)
260268
, add TopLevel "throw" throw_
261-
, add Normal "toJSON" prim_toJSON
262269
, add2 Normal "toFile" toFile
270+
, add Normal "toJSON" prim_toJSON
263271
, add Normal "toPath" toPath
264272
, add TopLevel "toString" toString
265273
, add Normal "toXML" toXML_
266274
, add2 TopLevel "trace" trace_
275+
, add0 Normal "true" (pure $ nvConstant $ NBool True)
267276
, add Normal "tryEval" tryEval
268277
, add Normal "typeOf" typeOf
278+
--, add0 Normal "unsafeDiscardOutputDependency" unsafeDiscardOutputDependency
279+
, add Normal "unsafeDiscardStringContext" unsafeDiscardStringContext
280+
, add2 Normal "unsafeGetAttrPos" unsafeGetAttrPos
269281
, add Normal "valueSize" getRecursiveSize
270-
, add Normal "getContext" getContext
271-
, add2 Normal "appendContext" appendContext
272-
273-
, add2 Normal "unsafeGetAttrPos" unsafeGetAttrPos
274-
, add Normal "unsafeDiscardStringContext" unsafeDiscardStringContext
275282
]
276283
where
277284
wrap :: BuiltinType -> Text -> v -> Builtin v
@@ -652,13 +659,13 @@ splitMatches numDropped (((_, (start, len)) : captures) : mts) haystack =
652659
thunkStr s = nvStr (hackyMakeNixStringWithoutContext (decodeUtf8 s))
653660

654661
substring :: forall e t f m. MonadNix e t f m => Int -> Int -> NixString -> Prim m NixString
655-
substring start len str = Prim $ if start < 0 --NOTE: negative values of 'len' are OK
656-
then
657-
throwError
658-
$ ErrorCall
659-
$ "builtins.substring: negative start position: "
660-
++ show start
661-
else pure $ principledModifyNixContents (Text.take len . Text.drop start) str
662+
substring start len str = Prim $
663+
if start < 0
664+
then throwError $ ErrorCall $ "builtins.substring: negative start position: " ++ show start
665+
else pure $ principledModifyNixContents (take . Text.drop start) str
666+
where
667+
--NOTE: negative values of 'len' are OK, and mean "take everything"
668+
take = if len < 0 then id else Text.take len
662669

663670
attrNames
664671
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
@@ -766,6 +773,12 @@ bitXor
766773
bitXor x y = fromValue @Integer x
767774
>>= \a -> fromValue @Integer y >>= \b -> toValue (a `xor` b)
768775

776+
builtinsBuiltin
777+
:: forall e t f m
778+
. MonadNix e t f m
779+
=> m (NValue t f m)
780+
builtinsBuiltin = (throwError $ ErrorCall "HNix does not provide builtins.builtins at the moment. Using builtins directly should be preferred")
781+
769782
dirOf :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
770783
dirOf x = demand x $ \case
771784
NVStr ns -> pure $ nvStr
@@ -1040,10 +1053,6 @@ isList
10401053
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
10411054
isList = hasKind @[NValue t f m]
10421055

1043-
isString
1044-
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
1045-
isString = hasKind @NixString
1046-
10471056
isInt
10481057
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
10491058
isInt = hasKind @Int
@@ -1060,6 +1069,12 @@ isNull
10601069
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
10611070
isNull = hasKind @()
10621071

1072+
-- isString cannot use `hasKind` because it coerces derivations to strings.
1073+
isString :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
1074+
isString v = demand v $ \case
1075+
NVStr{} -> toValue True
1076+
_ -> toValue False
1077+
10631078
isFunction :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
10641079
isFunction func = demand func $ \case
10651080
NVClosure{} -> toValue True

src/Nix/Effects.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ instance MonadInstantiate IO where
145145
++ err
146146

147147
pathExists :: MonadFile m => FilePath -> m Bool
148-
pathExists = doesFileExist
148+
pathExists = doesPathExist
149149

150150
class Monad m => MonadEnv m where
151151
getEnvVar :: String -> m (Maybe String)

src/Nix/Effects/Basic.hs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ defaultMakeAbsolutePath origPath = do
6464
Nothing -> getCurrentDirectory
6565
Just v -> demand v $ \case
6666
NVPath s -> pure $ takeDirectory s
67-
v ->
67+
val ->
6868
throwError
6969
$ ErrorCall
7070
$ "when resolving relative path,"
7171
++ " __cur_file is in scope,"
7272
++ " but is not a path; it is: "
73-
++ show v
73+
++ show val
7474
pure $ cwd <///> origPathExpanded
7575
removeDotDotIndirections <$> canonicalizePath absPath
7676

@@ -111,13 +111,13 @@ findEnvPathM name = do
111111
where
112112
nixFilePath :: MonadEffects t f m => FilePath -> m (Maybe FilePath)
113113
nixFilePath path = do
114-
path <- makeAbsolutePath @t @f path
115-
exists <- doesDirectoryExist path
116-
path' <- if exists
117-
then makeAbsolutePath @t @f $ path </> "default.nix"
118-
else pure path
119-
exists <- doesFileExist path'
120-
pure $ if exists then Just path' else Nothing
114+
absPath <- makeAbsolutePath @t @f path
115+
isDir <- doesDirectoryExist absPath
116+
absFile <- if isDir
117+
then makeAbsolutePath @t @f $ absPath </> "default.nix"
118+
else return absPath
119+
exists <- doesFileExist absFile
120+
pure $ if exists then Just absFile else Nothing
121121

122122
findPathBy
123123
:: forall e t f m
@@ -226,13 +226,13 @@ findPathM
226226
=> [NValue t f m]
227227
-> FilePath
228228
-> m FilePath
229-
findPathM = findPathBy path
229+
findPathM = findPathBy existingPath
230230
where
231-
path :: MonadEffects t f m => FilePath -> m (Maybe FilePath)
232-
path path = do
233-
path <- makeAbsolutePath @t @f path
234-
exists <- doesPathExist path
235-
pure $ if exists then Just path else Nothing
231+
existingPath :: MonadEffects t f m => FilePath -> m (Maybe FilePath)
232+
existingPath path = do
233+
apath <- makeAbsolutePath @t @f path
234+
exists <- doesPathExist apath
235+
pure $ if exists then Just apath else Nothing
236236

237237
defaultImportPath
238238
:: (MonadNix e t f m, MonadState (HashMap FilePath NExprLoc) m)

src/Nix/Exec.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,12 @@ callFunc fun arg = demand fun $ \fun' -> do
297297
when (length frames > 2000) $ throwError $ ErrorCall
298298
"Function call stack exhausted"
299299
case fun' of
300-
NVClosure params f -> do
301-
traceM $ "callFunc:NVFunction taking " ++ show params
300+
NVClosure _params f -> do
302301
f arg
303302
NVBuiltin name f -> do
304303
span <- currentPos
305304
withFrame Info (Calling @m @t name span) (f arg)
306305
s@(NVSet m _) | Just f <- M.lookup "__functor" m -> do
307-
traceM "callFunc:__functor"
308306
demand f $ (`callFunc` s) >=> (`callFunc` arg)
309307
x -> throwError $ ErrorCall $ "Attempt to call non-function: " ++ show x
310308

@@ -316,7 +314,6 @@ execUnaryOp
316314
-> NValue t f m
317315
-> m (NValue t f m)
318316
execUnaryOp scope span op arg = do
319-
traceM "NUnary"
320317
case arg of
321318
NVConstant c -> case (op, c) of
322319
(NNeg, NInt i ) -> unaryOp $ NInt (-i)
@@ -478,7 +475,7 @@ execBinaryOpForced scope span op lval rval = case op of
478475
fromStringNoContext :: Framed e m => NixString -> m Text
479476
fromStringNoContext ns = case principledGetStringNoContext ns of
480477
Just str -> pure str
481-
Nothing -> throwError $ ErrorCall "expected string with no context"
478+
Nothing -> throwError $ ErrorCall $ "expected string with no context, but got " ++ show ns
482479

483480
addTracing
484481
:: (MonadNix e t f m, Has e Options, MonadReader Int n, Alternative n)

src/Nix/Parser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ nixIf = annotateLocation1
268268
nixAssert :: Parser NExprLoc
269269
nixAssert = annotateLocation1
270270
( NAssert
271-
<$> (reserved "assert" *> nixExpr)
271+
<$> (reserved "assert" *> nixToplevelForm)
272272
<*> (semi *> nixToplevelForm)
273273
<?> "assert"
274274
)

src/Nix/Render.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ posAndMsg (SourcePos _ lineNo _) msg = FancyError
7777

7878
renderLocation :: MonadFile m => SrcSpan -> Doc a -> m (Doc a)
7979
renderLocation (SrcSpan (SourcePos file begLine begCol) (SourcePos file' endLine endCol)) msg
80+
| file == file' && file == "<string>" && begLine == endLine
81+
= pure $ "In raw input string at position " <> pretty (unPos begCol)
82+
8083
| file /= "<string>" && file == file'
8184
= do
8285
exist <- doesFileExist file

src/Nix/Thunk/Basic.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import Control.Exception hiding ( catch )
1515
import Control.Monad.Catch
1616

1717
import Nix.Thunk
18-
import Nix.Utils
1918
import Nix.Var
2019

2120
data Deferred m v = Deferred (m v) | Computed v
@@ -75,7 +74,6 @@ forceThunk (Thunk n active ref) k = do
7574
if nowActive
7675
then throwM $ ThunkLoop $ show n
7776
else do
78-
traceM $ "Forcing " ++ show n
7977
v <- catch action $ \(e :: SomeException) -> do
8078
_ <- atomicModifyVar active (False, )
8179
throwM e

0 commit comments

Comments
 (0)